Using sorcery in rspec NoMethodError: undefined method `authenticates_with_sorcery!'
Using sorcery in rspec NoMethodError: undefined method `authenticates_with_sorcery!'
大家好,我正在尝试测试一个必须对用户进行身份验证的控制器,但我一直收到 NoMethodError: undefined method `authenticates_with_sorcery!'
,即使控制器本身和用户模型也能正常工作。所以基本上:如果我在服务器上执行操作,如果我 运行 rake spec
它不会。
用户模型
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
用户工厂
FactoryGirl.define do
factory :user do
email "whatever@whatever.com"
password "password"
password_confirmation "password"
end
end
控制器规格
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
需要 'factory_girl_rails'
需要 '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
如果我现在 运行 rake spec
我得到:
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)>'
非常感谢任何帮助!
我想我发现了问题:
Factory Girl 不喜欢用巫术工作,如果我用制造商代替工厂:
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
效果很好。
大家好,我正在尝试测试一个必须对用户进行身份验证的控制器,但我一直收到 NoMethodError: undefined method `authenticates_with_sorcery!'
,即使控制器本身和用户模型也能正常工作。所以基本上:如果我在服务器上执行操作,如果我 运行 rake spec
它不会。
用户模型
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
用户工厂
FactoryGirl.define do
factory :user do
email "whatever@whatever.com"
password "password"
password_confirmation "password"
end
end
控制器规格
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 需要 'factory_girl_rails' 需要 '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
如果我现在 运行 rake spec
我得到:
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)>'
非常感谢任何帮助!
我想我发现了问题: Factory Girl 不喜欢用巫术工作,如果我用制造商代替工厂:
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
效果很好。