rspec - 将 RSpec 2 "to have().errors_on"匹配器转换为 RSpec 3

标签 rspec rspec-rails rspec3

在我的 Gemfile 中

gem 'rspec-rails', '~> 3.3'

我发现,errors_on 匹配器移动到了另一个 gem:https://github.com/rspec/rspec-collection_matchers

我确信一定有一种方法可以在 RSpec 3 中实现相同的目标。

如何将 RSpec 2 have().errors_on 匹配器转换为 RSpec 3?

  expect {
    click_button(I18n.t('helpers.submit.accounting.update'))
  }.to have(4).errors_on(:share_ksk, :central_office, :limit_value, :fix_disagio)

我试过:

  expect {
    click_button(I18n.t('helpers.submit.accounting.update'))
  }.to have_validation_error(I18n.t('accounting.two_digits_after_decimal_point')).on(
:share_ksk, 
:central_office, 
:limit_value, 
:fix_disagio)

关于 https://github.com/rspec/rspec-rails/issues/1033

但我收到 RSpec 错误:

 undefined method `on' for #<RSpec::Matchers::BuiltIn::Has:0x00000005913368>

最佳答案

解决方案 1: 如您所知,您可以使用 rspec-collection_matchers gem。

解决方案 2:您可以猴子修补 errors_on::ActiveModel::Validations 上,如上所示 here

关于rspec - 将 RSpec 2 "to have().errors_on"匹配器转换为 RSpec 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33777521/

相关文章:

ruby - 如何在 cucumber 步骤定义之外使用 rspec-expectations

ruby-on-rails - Rack::Test 导致 ActiveRecord::AssociationTypeMismatch

ruby-on-rails - 你如何在 Rails 中使用 Rspec 测试你的配置/初始化脚本?

ruby-on-rails-4 - ActionController::TestResponse 不响应方法 has_selector?

ruby-on-rails - RSpec中的 "be_true"和 "be true"有什么区别

ruby-on-rails-4 - RSpec 3.4/Rails 4.2 : how to test view containing button_to

ruby-on-rails - 带有 spork 的未初始化常量 RSpec

Rspec 双重期望/允许任何事情

ruby-on-rails - 如何测试优惠券/促销代码的唯一性?

ruby-on-rails - Rspec极慢