我尝试测试巫术宝石的登录方法,但我收到错误。 我在我的rspec测试中为工厂用户工厂女孩。 我想要做的就是添加一个登录我的before方法,之后我想测试控制器的操作。
我的spec_helper.rb
require 'factory_girl'
require_relative '../spec/factories/blog.rb'
require_relative '../spec/factories/user.rb'
require_relative '../spec/factories/category.rb'
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
config.include Sorcery::TestHelpers::Rails::Controller, type: :controller
config.include Sorcery::TestHelpers::Rails::Integration, type: :feature
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
end
我的rails_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
RSpec.configure do |config|
config.infer_spec_type_from_file_location!
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
end
运行我的rspec时得到的错误
Running: spec
/home/user/work/project_name/spec/spec_helper.rb:9:in `block in <top (required)>': uninitialized constant Sorcery::TestHelpers::Rails::Controller (NameError)
我的工作环境:
Rails 3.2, Ruby 1.9.3, rspec - rails 3.3, sorcery 0.8.2
答案 0 :(得分:0)
我在经过数小时的痛苦后发现了这个问题。
问题在于对于巫术版本0.8.2,您需要在spec_helper.rb中添加以下代码:
RSpec.configure do |config|
.................................(other stuff)
config.include Sorcery::TestHelpers::Rails
..........................(other stuff)
end
在您的规格中使用之后
@user = User.create
login_user(@user)