2016-04-27 85 views
0

'shoulda-matchers'gem在運行rspec測試時未加載。Rails未能加載shoulda-matchers 3.1.1 gem,未定義的方法`allow_value'for#<RSpec

錯誤:正在使用 NoMethodError: undefined method 'allow_value' for #<RSpec::ExampleGroups::Contact::ActiveModelValidationss:0x007fef1021b310>

版本: Ruby 2.2.2Rails 4.2.1 shoulda-matchers 3.1.1

的Gemfile:

source 'https://rubygems.org' 

gem 'rails', '4.2.1' 
gem 'sqlite3' 
gem 'sass-rails', '~> 5.0' 
gem 'uglifier', '>= 1.3.0' 
gem 'coffee-rails', '~> 4.1.0' 
gem 'jquery-rails' 
gem 'turbolinks' 
gem 'jbuilder', '~> 2.0' 
gem 'sdoc', '~> 0.4.0', group: :doc 

group :development, :test do 
    gem 'byebug' 
    gem 'rspec-rails', '~> 3.0' 
    gem 'factory_girl_rails' 
    gem 'shoulda-matchers', '~> 3.1' 
    gem 'web-console', '~> 2.0' 
    gem 'spring' 
    gem 'rubocop', require: false 
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' 

ActiveRecord::Migration.maintain_test_schema! 

RSpec.configure do |config| 
    config.fixture_path = "#{::Rails.root}/spec/fixtures" 
    config.use_transactional_fixtures = true 
    config.infer_spec_type_from_file_location! 
    config.filter_rails_from_backtrace! 

    Shoulda::Matchers.configure do |config| 
    config.integrate do |with| 
     with.test_framework :rspec 
     with.library :active_record 
     with.library :rails 
    end 
    end 
end 

spec_helper.rb:

RSpec.configure do |config| 
    require 'factory_girl_rails' 
    require 'shoulda/matchers' 
    config.include FactoryGirl::Syntax::Methods 
    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 

contact_spec.rb:

require 'spec_helper' 

describe Contact do 

    FactoryGirl.define do 
    factory :contact do 
     first_name "Alice" 
     last_name "Bob" 
     email_address "[email protected]" 
     phone_number "555-555-5555" 
     company_name "ExampleCompany" 
    end 
    end 

    it "has a valid factory" do 
    expect(build(:contact)).to be_valid 
    end 

    describe "ActiveModel validations" do 
    it { expect(:contact).to allow_value("[email protected]").for(:email_address) } 
    end 
end 

對不起,所有的代碼。請注意我正在使用shoulda-matchers 3.1.1版本,並且不再需要在gemfile中添加'require:false'https://robots.thoughtbot.com/shoulda-matchers-3-0

我也曾嘗試將gentfile中的shoulda-matchers添加到自己的測試環境中如下所示,但沒有發現變化

group :test do 
    gem 'shoulda-matchers', '~> 3.1' 
end 

我在網上查找過,發現類似問題的解決方案不適用於我的情況。任何幫助或建議都會很棒!

+0

你設法解決您的問題? –

回答

0

,應該讓你再次綠色的修復方法是:

it { expect(:contact).to allow_value("[email protected]").for(:email_address) }

it { expect(build(:contact)).to allow_value("[email protected]").for(:email_address) }

以同樣的方式爲你的第一次測試,因爲你的第二個測試是期待符號:contactallow_value,而不是在工廠中創建的對象。

此外,我建議你的工廠定義移動到自己的文件spec/factories/contact.rb下,而不是在你的規格在線,並memoise在let方法的接觸對象,所以你有一個規範,看起來像:

require 'spec_helper' 

describe Contact do 
    let(:contact) { build(:contact) } 

    it "has a valid factory" do 
    expect(contact).to be_valid 
    end 

    describe "ActiveModel validations" do 
    it { expect(contact).to allow_value("[email protected]").for(:email_address) } 
    end 
end 
0

在我的情況下,問題是,測試類不是一個ActiveRecord項目,所以shoulda沒有加載的方法,如allow_value默認。

的解決辦法是在_spec.rb文件中添加type: :model

RSpec.describe MyTestedClass, type: :model do 
    ... 
end 
相關問題