ruby-on-railsrubyruby-on-rails-4rspecsorcery

Using sorcery in rspec NoMethodError: undefined method `authenticates_with_sorcery!'


Hey everyone I am trying to test a controller in which the user has to be authenticated, but I keep getting the NoMethodError: undefined method `authenticates_with_sorcery!' even though the controller in itself works and user model too. So basically: If I do the action on the server it works if I run rake spec it doesnt.

User model

class User < ActiveRecord::Base
  authenticates_with_sorcery!

  validates :password, presence: true, confirmation: true, length: { minimum: 8 }
  validates :email, presence: true, uniqueness: true
  validates :password_confirmation, presence: true
end

User Factory

FactoryGirl.define do
  factory :user do
    email "whatever@whatever.com"
    password "password"
    password_confirmation "password"
  end
end

controller spec

require 'rails_helper'

RSpec.describe Admin::ImagesController, :type => :controller do

  before(:each) do
    create(:user)
    login_user_post("whatever@whatever.com", "secret")
  end

  describe "GET 'new'" do
    it "returns http success" do
      get 'new', venue_id: 1
      expect(response).to be_success
    end
  end
end

spec_helper.rb require 'factory_girl_rails' require 'sorcery'

RSpec.configure do |config|
  config.include FactoryGirl::Syntax::Methods
  config.include Sorcery::TestHelpers::Rails
  config.include Sorcery::TestHelpers::Rails::Controller, type: :controller
  config.include Sorcery::TestHelpers::Rails::Integration, type: :feature
end

If I now run rake spec I get:

1) Admin::ImagesController GET 'new' returns http success
 Failure/Error: create(:user)
 NoMethodError:
   undefined method `authenticates_with_sorcery!' for #<Class:0x007fc1221c1430>
 # ./app/models/user.rb:2:in `<class:User>'
 # ./app/models/user.rb:1:in `<top (required)>'
 # ./spec/controllers/admin/images_controller_spec.rb:6:in `block (2 levels) in <top (required)>'

Any help is much appreciated!


Solution

  • I think I found the issue: Factory Girl doesnt like to work with sorcery, if I replace the Factory, with a fabricator:

    Fabricator(:user, :class_name => "User") do
      id { sequence }
      password { "secret" }
      email { "whatever@whatever.com" }
      salt { "asdasdastr4325234324sdfds" }
      crypted_password { Sorcery::CryptoProviders::BCrypt.encrypt("secret", "asdasdastr4325234324sdfds") }
    end
    

    It works quite well.