Ruby User Authentication with Rails Password Hashing and Sign In Creating the Sessions Controller

Using `stub` from rspec-mocks' old `:should` syntax without explicitly enabling the syntax is deprecated

Hello Folks,

I'm trying to figure out how to get rid of this deprecation warning:

Deprecation Warnings:

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/*/treehouse/projects/odot/spec/controllers/user_sessions_controller_spec.rb:32:in `block (4 levels) in <top (required)>'.

here's the code from my user_sessions_controller_spec.rb:

it "authenticates the user" do
        User.stub(:find_by).and_return(user)
        expect(user).to receive(:authenticate)
        post :create, email: "jabba@thehut.com", password: "jabba12345"
     end

Thanks in advance!! Ogechi

3 Answers

I got it to work, sans deprecation errors by using this:

allow(User).to receive(:find_by).and_return(user)

instead of this:

User.stub(:find_by).and_return(user)

based on this syntax: rspec 3 - stub a class method

I think this is the new syntax:

allow(User).to receive_message_chain(:find_by).and_return(user)
expect(user).to receive(:authenticate)
post :create, email: 'email@email.com', password: 'P@$$WORD'

basically, i don't know how to update to the new syntax as specified: http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3

# old syntax:
object.stub(:foo => 1, :bar => 2)
# new syntax:
allow(object).to receive_messages(:foo => 1, :bar => 2)

# old syntax:
object.stub_chain(:foo, :bar, :bazz).and_return(3)
# new syntax:
allow(object).to receive_message_chain(:foo, :bar, :bazz).and_return(3)