Search code examples
ruby-on-railsrspecsorcery

Sorcery fails to log in in RSpec


I have an app that uses Sorcery (or tries to use it), and I am writing the specs for it:

context "successfull attempts to log in" do
  let(:attr) { attributes_for(:credentials) }

  before(:each) do
    @user = create(:user, attr)
  end

  it "should log the user in" do
    post :create, attr.merge(remember_me: false)
    controller.should be_logged_in
  end
end

Here is the FactoryGirl factory:

FactoryGirl.define do
  factory :user do
    email                 Faker::Internet.safe_email
    password              "password"
    password_confirmation { |u| u.password }
    client_id             1
  end

  factory :credentials, class: User do
    email       "[email protected]"
    password    "password"
  end
end

And here is the controller action:

class SessionsController < ApplicationController
  # ...
  def create
    login(params[:email], params[:email], params[:remember_me])
    flash.now[:error] = "Invalid email/password combination"
    render :new
  end
end

The error message is the following:

1) SessionsController POST 'create' successfull attempts to log in should log the user in
     Failure/Error: controller.should be_logged_in
       expected logged_in? to return true, got false
     # ./spec/controllers/sessions_controller_spec.rb:54:in `block (4 levels) in <top (required)>'

The spec keeps failing for some reason. Could anyone explain to me why please?


Solution

  • Your controller uses the email as both the username and password, which looks like a copy and paste error. Is that correct?