我正在尝试测试一个padrino控制器,它依赖于Padrino提供的current_account :: Admin :: AccessControl
为此,我需要模拟current_account.
代码是这样的:
App.controller :post do post :create,map => '/create' do Post.create :user => current_account end end
和rspec:
describe "Post creation" do it 'should create' do account = Account.create :name => 'someone' loggin_as account #to mock current_account post '/create' Post.first.user.should == account end end
如何实现“loggin_as”或如何编写此测试?
解决方法
我发现了一种简单的测试方法:
App.any_instance.stub(:current_account).and_return(account)
所以,测试代码应该是:
describe "Post creation" do it 'should create' do account = Account.create :name => 'someone' App.any_instance.stub(:current_account).and_return(account) post '/create' Post.first.user.should == account end end
但我还是想建立“loggin_as”帮手.那么,我该如何动态获取App类? (我应该为这个问题创建另一个线程吗?)