私はRyanBatesを使用nifty:authentication
しており、Rspecでテストを開始しています。何週間もこれと戦いましたが、それでも何が起こっているのか理解できません。
私のコントローラーは単に呼び出します
before_filter :login_required, :except => [:login]
これはlib/controller_authenticationで定義されています
def self.included(controller)
controller.send :helper_method, :current_account, :logged_in?, :redirect_to_target_or_default
end
def current_account
@current_account ||= Account.find(session[:account_id]) if session[:account_id]
end
def logged_in?
current_account
end
def login_required
unless logged_in?
store_target_location
redirect_to login_url, :alert => "You must first answer me these riddles three log in or sign up before accessing this page."
end
end
def redirect_to_target_or_default(default, *args)
redirect_to(session[:return_to] || default, *args)
session[:return_to] = nil
end
private
def store_target_location
session[:return_to] = request.url
end
end
アプリは意図したとおりに動作しますが、テストは毎回失敗します。何をしようとも、redirect_to login_url, :alert => "You must first ...log in"
ページが表示されます。
私が試したこと:
controller.stub!( :login_required )
ControllerAuthentication.stub!(:current_account).and_return(Account.where(:username => 'ej0c').first)
#ControllerAuthentication.stub!(:logged_in?).and_return(Account.where(:username => 'ej0c').first)
ControllerAuthentication.stub!(:login_required).and_return(true)
MyDigisController.stub!( :login_required ).and_return(true)
これは、私が物事の理論全体を見逃していることを意味すると思います。ログインを機能させるにはどうすればよいですか?
Punitが以下に示唆するように私は試しました:[pre]
require 'spec_helper'
describe "View event details" do
it "Should show a table of events" do
@account = Account.where(:username => 'ej0c').first
puts @account.inspect
controller.stub!(:current_account).and_return(@account)
controller.stub!(:logged_in?).and_return(true)
session[:account_id] = @account.id
visit '/my_digis/66/custom_events'
page.should have_content('Events')
end
end
@ account.inspectはうまく表示されましたが、私も
An expectation of :current_account was set on nil. Called from C:/Users/Ed/webapps/whendidji3/spec/con
.rb:8:in `block (2 levels) in <top (required)>'. Use allow_message_expectations_on_nil to disable warn
An expectation of :logged_in? was set on nil. Called from C:/Users/Ed/webapps/whendidji3/spec/controll
:in `block (2 levels) in <top (required)>'. Use allow_message_expectations_on_nil to disable warnings.
何が起こっているのかを理解するために高低を検索したので、詳細な説明をありがとう。