我正在尝试测试依赖于Padrino提供的current_account的padrino控制器:: 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”或如何编写此测试?
答案 0 :(得分:3)
我找到了一种简单的测试方法:
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类? (我应该为这个问题创建另一个线程吗?)