5

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?

cpereira
  • 155
  • 9

1 Answers1

3

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?)

cpereira
  • 155
  • 9
  • 3
    gotcha. To get app class just use "app" instead of "App" – cpereira Nov 21 '12 at 19:25
  • Sure cpeireira ? Because I get : Failure/Error: app.any_instance.stub(:current_account).and_return(@admin) NoMethodError: undefined method `any_instance' for # – leucos Apr 05 '14 at 14:38
  • 1
    what rspec and padrino versions are you using? – cpereira Apr 09 '14 at 00:42
  • 1
    padrino 0.12.0 and rspec 2.14. I finally ended up doing it this way : https://gist.github.com/leucos/10231670 - Thanks ! – leucos Apr 09 '14 at 06:27