2015-10-22 17 views
3

Rails 4.2.4,Rspec 3.3.1,shoulda-matchers 3.0.0。應用程序匹配器不被rails/rspec

我越來越

#... 
    358) Participant validations 
     Failure/Error: it { should ensure_length_of(:coresp_country).is_at_most(255) } 
     NoMethodError: 
     undefined method `ensure_length_of' for #<RSpec::ExampleGroups::Participant::Validations:0x0000000f40aec0> 
     # ./spec/models/participant_spec.rb:100:in `block (3 levels) in <top (required)>' 

    359) Participant validations company 
     Failure/Error: it { should ensure_length_of(:company).is_at_most(255) } 
     NoMethodError: 
     undefined method `ensure_length_of' for #<RSpec::ExampleGroups::Participant::Validations::Company:0x0000000f414ab0> 
     # ./spec/models/participant_spec.rb:149:in `block (4 levels) in <top (required)>' 

    360) Participant validations company declared_type = COMPANY 
     Failure/Error: it { should validate_presence_of(:company) } 
     NoMethodError: 
     undefined method `validate_presence_of' for #<RSpec::ExampleGroups::Participant::Validations::Company::DeclaredTypeCOMPANY:0x0000000f429c58> 
    #... 

而這種更多的失敗(貌似早該-匹配器不工作)。

rails_helper.rb:

ENV['RAILS_ENV'] ||= 'test' 
require File.expand_path('../../config/environment', __FILE__) 
require 'spec_helper' 
require 'rspec/rails' 
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} 

Rails.logger.level = 4 

ActiveRecord::Migration.maintain_test_schema! 

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

    config.include FactoryGirl::Syntax::Methods 
    config.include Sorcery::TestHelpers::Rails 
    config.include Macros::Controller::Security 
end 
FactoryGirl.reload 
Shoulda::Matchers.configure do |config| 
    config.integrate do |with| 
    with.test_framework :rspec 
    with.library :rails 
    end 
end 

spec_helper.rb:

require 'simplecov_helper' 
require 'webmock/rspec' 
WebMock.disable_net_connect!(allow_localhost: true) 
require 'rspec/collection_matchers' 

RSpec.configure do |config| 
    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 
    config.filter_run :focus 
    config.run_all_when_everything_filtered = true 
    config.disable_monkey_patching! 
    config.expose_dsl_globally = true 
    config.default_formatter = 'doc' if config.files_to_run.one? 
    config.order = :random 

    Kernel.srand config.seed 
end 

編輯

好吧,我認爲這個問題是不是早該-的匹配,但與active_attr gem ,因爲測試只在spec/compositions/api文件夾中失敗,我使用該寶石。

+0

對不起,在最明顯的情況下可以戳一下,但在給定的規範中需要rails_helper.rb嗎?也許你已經意外地需要spec_helper.rb – jfornoff

+0

'rails_helper.rb'是必需的:) –

+0

好的,下一個檢查點:否require:false在gem'shoulda-matchers'中? ;-) – jfornoff

回答

7

shoulda-matchers 3.0有選擇地使其匹配器可用。我使用的ActiveModel匹配器僅混合到模型規範中(af98a23)。

我有兩個選擇來解決這一問題:

  • 放置模型,使用active_attr寶石下spec/models文件夾的功能;
  • type: :model添加到spec/compositions/api文件夾中的每個類的頂級描述塊。

我決定去第二個選項,它的工作。


旁註

現在匹配器,這與保證(ensure_inclusion_inensure_length_of)開始重命名爲validate_inclusion_invalidate_length_of55c8d09)。

+2

感謝你(尤其是旁註)。保存我的理智 –

+1

@JayDorsey我記得當我最終解決這個問題的時候,我正要解僱我:D很高興這對我以外的人有幫助! –

相關問題