【问题标题】:Shoulda-matchers are not seen by rails/rspecrails/rspec 看不到应该匹配器
【发布时间】:2016-01-21 17:08:56
【问题描述】:

Rails 4.2.4、Rspec 3.3.1、shouda-matchers 3.0.0。

我得到了

  #...
  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

编辑

好的,我认为问题不在于 shoulda-matchers,而在于 active_attr gem,因为测试仅在我使用 gem 的 spec/compositions/api 文件夹中失败。

【问题讨论】:

  • 对不起,在最明显的情况下有点戳,但是在给定的规范中是否需要 rails_helper.rb ?也许你不小心需要 spec_helper.rb
  • rails_helper.rb 是必需的 :)
  • 好的,下一个检查点:在 gem 'should-matchers' 中不需要:false? ;-)
  • require: false 我得到uninitialized constant Shoulda (NameError) rails_helper.rb 中指向Shoulda::Matchers.configure
  • 这是有道理的,只是想确保 require: false 不在那里,一些教程建议你这样做,然后手动要求它。我不完全确定捆绑 gem 的 require 机制如何在那里工作,但我认为在 Gemfile 中的 test-group 中有“should-matchers”gem 很重要,是否也检查过?

标签: ruby-on-rails rspec rspec-rails ruby-on-rails-4.2 shoulda


【解决方案1】:

shoulda-matchers 3.0 选择性地使其匹配器可用。我正在使用仅混入模型规格 (af98a23) 的 ActiveModel 匹配器。

我有两个选择来解决这个问题:

  • 将使用active_attr gem 的模型的规格放在spec/models 文件夹下;
  • type: :model 添加到spec/compositions/api 文件夹中每个类的顶级描述块中。

我决定选择第二个选项,它奏效了。


旁注

现在以 ensure (ensure_inclusion_in, ensure_length_of) 开头的匹配器被重命名为 validate_inclusion_in, validate_length_of (55c8d09)。

【讨论】:

  • 感谢您(尤其是旁注)。拯救了我的理智
  • @JayDorsey 我记得我正要松开我的时候终于解决了:D 很高兴它对我以外的人有帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多