sorcery icon indicating copy to clipboard operation
sorcery copied to clipboard

Unable to use sorcery in rspec

Open timoroemer opened this issue 10 years ago • 6 comments

Heyho,

I am getting a ``method_missing': undefined method authenticates_with_sorcery!' when I am trying to create a user using the FactoryGirl gem in rspec. I googled and tried and still failing - I hope I am just missing sth obvious:

gem 'rails', '4.1.4'
gem 'sorcery' (0.9.1)
gem 'factory_girl_rails'

rails_helper.rb

ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'

Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
  config.use_transactional_fixtures = true
  config.infer_spec_type_from_file_location!
end

spec_helper.rb

require 'factory_girl_rails'
require 'sorcery'

RSpec.configure do |config|
  config.include FactoryGirl::Syntax::Methods
  config.include Sorcery::TestHelpers::Rails::Controller, type: :controller
  config.filter_run :focus
  config.run_all_when_everything_filtered = true
  if config.files_to_run.one?
    config.default_formatter = 'doc'
  end
  config.profile_examples = 10
  config.order = :random
  Kernel.srand config.seed
  config.expect_with :rspec do |expectations|
    expectations.syntax = :expect
  end
    mocks.verify_partial_doubles = true
  end
end

spec.rb

require 'rails_helper'

RSpec.describe Admin::ImagesController, :type => :controller do
  before(:each) do
    create(:user)
    login_user_post("[email protected]", "secret")
  end

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

user_factory.rb

FactoryGirl.define do
  factory :user do
    email "[email protected]"
    password "password"
    password_confirmation "password"
  end
end

/support/sorcery_module.rb

module Sorcery
  module TestHelpers
    module Rails
      module Integration
        def login_user_post(user, password)
          page.driver.post(user_sessions_url, { username: user, password: password})
        end
      end
    end
  end
end

I hope this includes all the information - I am still quite new to rails and especially writing tests... Help is much appreciated!

timoroemer avatar Jul 20 '15 23:07 timoroemer

Hi @timoroemer, did you run rails generate sorcery:install?

arnvald avatar Jul 22 '15 12:07 arnvald

I believe I did. The authentication works actually. It is just the rspec test/the factory that is complaining. So I can actually log in on my server and log out - all good there.

Any idea why rspec is complaining?

timoroemer avatar Jul 22 '15 15:07 timoroemer

I double checked everything I could think of and still it is not working. Even after rails g sorcery:install again.

Any ideas?

timoroemer avatar Jul 26 '15 12:07 timoroemer

Alright - I searched and I think I found the issue: FactoryGirl doesnt seem to like "authenticates_with_sorcery!" much. When I use a fabricator for some magical reason or the other this will work just fine (getting: undefined local variable or method page' for #RSpec::ExampleGroups::AdminVenueImagesController:0x007f9fd2cb65c8` error now, but maybe I can find that out why that is too).

If you dont have any questions or input, you can close the issue.

timoroemer avatar Jul 26 '15 21:07 timoroemer

I was wrong. Still broken.

timoroemer avatar Jul 27 '15 18:07 timoroemer

@timoroemer I can't really help. Yesterday I tried to reproduce it and I could easily create users with FactoryGirl, so I have no idea what causes the problem in your case. Maybe in your test environment you are loading Sorcery before Rails? In such case Sorcery will not add authenticates_with_sorcery! method to ActiveRecord::Base

arnvald avatar Jul 29 '15 01:07 arnvald