I am using Rails4 and devise. While writing controller specs, I came across this code:
user = double(@user)
request.env['warden'].stub :authenticate! => user
allow(controller).to receive(:current_user) { user }
... here.
It works. The controller specs I wrote that require authentication pass appropriately.
I've read some web entries about how a stub works, and I think I get it. Seems fast and adequate for my needs.
Even though the above code uses the RSpec double
method, I still get this depreciation warning:
Using
stub
from rspec-mocks' old:should
syntax without explicitly enabling the syntax is deprecated. Use the new:expect
syntax or explicitly enable:should
instead. Called from /Users/perry_mac/rails_projects/mymri/spec/controllers/steps_controller_spec.rb:14:in `block (2 levels) in '.
Which puzzles me since I am using the "new :expect syntax." So I am left with a vague unease about this otherwise working code snippet. For instance, it is not immediately clear how I could use it to write a test that makes use of two users logging in and ensuring each is seeing only their appropriately scoped search results. Is there a more suitable way to stub a session to check controllers that make use of authentication?
And... why does the above work, but something like this does not?:
before(:each) {
email = "[email protected]"
password= "password"
@user = FactoryGirl.create(:user, email: email, password: password)
session[:user_id] = @user.id
.
.
.