Mock current_account for Padrino for rspec test

I am trying to test a padrino controller which depends on the current_account provided by Padrino :: Admin :: AccessControl

To do this, I need mock current_account.

the code looks something like this:

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

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

+4
source share
1 answer

I found an easy way to check:

 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 create the loggin_as helper. So how can I dynamically get an App class? (should I create another thread for this question?)

+3
source

Source: https://habr.com/ru/post/1447557/


All Articles