Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mock current_account on Padrino for rspec test

I'm trying to test a padrino controller that depends on current_account provided by Padrino::Admin::AccessControl

To do so, I need to mock current_account.

the code is something like:

App.controller :post do
  post :create, map => '/create' do
    Post.create :user => current_account
  end
end

and the 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

How can I implement "loggin_as" or how can I write this test?

like image 674
cpereira Avatar asked Oct 23 '22 01:10

cpereira


1 Answers

I found a simple way to test:

App.any_instance.stub(:current_account).and_return(account)

So, the test code should be:

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

but I still like to build "loggin_as" helper. So, how can I dynamically get App class? (should I create another thread for this question?)

like image 53
cpereira Avatar answered Nov 02 '22 12:11

cpereira