ChatGPT解决这个技术问题 Extra ChatGPT

RSpec:期望更改多个

在提交功能规范中的表单时,我想检查模型中的许多更改。例如,我想确保用户名从 X 更改为 Y,并且加密密码已更改为任何值。

我知道已经有一些问题,但我没有找到适合我的答案。最准确的答案似乎是 Michael Johnston 的 ChangeMultiple 匹配器:Is it possible for RSpec to expect change in two tables?。它的缺点是只检查从已知值到已知值的显式变化。

我创建了一些关于我认为更好的匹配器看起来如何的伪代码:

expect {
  click_button 'Save'
}.to change_multiple { @user.reload }.with_expectations(
  name:               {from: 'donald', to: 'gustav'},
  updated_at:         {by: 4},
  great_field:        {by_at_leaset: 23},
  encrypted_password: true,  # Must change
  created_at:         false, # Must not change
  some_other_field:   nil    # Doesn't matter, but want to denote here that this field exists
)

我还创建了 ChangeMultiple 匹配器的基本骨架,如下所示:

module RSpec
  module Matchers
    def change_multiple(receiver=nil, message=nil, &block)
      BuiltIn::ChangeMultiple.new(receiver, message, &block)
    end

    module BuiltIn
      class ChangeMultiple < Change
        def with_expectations(expectations)
          # What to do here? How do I add the expectations passed as argument?
        end
      end
    end
  end
end

但现在我已经收到此错误:

 Failure/Error: expect {
   You must pass an argument rather than a block to use the provided matcher (nil), or the matcher must implement `supports_block_expectations?`.
 # ./spec/features/user/registration/edit_spec.rb:20:in `block (2 levels) in <top (required)>'
 # /Users/josh/.rvm/gems/ruby-2.1.0@base/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
 # /Users/josh/.rvm/gems/ruby-2.1.0@base/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load'

非常感谢创建此自定义匹配器的任何帮助。


M
Maksim Kalmykov

在 RSpec 3 中,您可以一次设置多个条件(因此不会破坏单一期望规则)。它看起来像:

expect {
  click_button 'Save'
  @user.reload
}.to change { @user.name }.from('donald').to('gustav')
 .and change { @user.updated_at }.by(4)
 .and change { @user.great_field }.by_at_least(23}
 .and change { @user.encrypted_password }

虽然它不是一个完整的解决方案 - 就我的研究而言,还没有简单的方法可以做到 and_not。我也不确定你的最后一次检查(如果没关系,为什么要测试它?)。当然,您应该能够将其包装在您的 custom matcher 中。


如果您希望多项内容不会更改,只需使用 .and change { @something }.by(0)
您可以添加第二个带有所有括号的示例吗?我很难理解哪些方法是链式的
My answer 适用于 Ruby 2,并且似乎适用于 .should_not 以供任何需要它的人使用
M
Matthew Hinea

如果要测试多个记录是否未更改,可以使用 RSpec::Matchers.define_negated_matcher 反转匹配器。所以,添加

RSpec::Matchers.define_negated_matcher :not_change, :change

到您的文件顶部(或到您的 rails_helper.rb),然后您可以使用 and 链接:

expect{described_class.reorder}.to not_change{ruleset.reload.position}.
    and not_change{simple_ruleset.reload.position}

C
CupawnTae

BroiSatse's answer 是最好的,但如果您使用 RSpec 2(或有更复杂的匹配器,如 .should_not),此方法也适用:

lambda {
  lambda {
    lambda {
      lambda {
        click_button 'Save'
        @user.reload
      }.should change {@user.name}.from('donald').to('gustav')
    }.should change {@user.updated_at}.by(4)
  }.should change {@user.great_field}.by_at_least(23)
}.should change {@user.encrypted_password}

啊,好主意!您可能可以构建一个包装器以使其更易于阅读!
F
Foo Bar Zoo

由于 RSpec 版本 3.1.0 中添加了对 change {}完整复合匹配器支持,因此接受的答案并非 100% 正确。如果您尝试使用 RSpec 3.0 版运行接受的答案中给出的代码,则会收到错误消息。

为了使用带有 change {} 的复合匹配器,有两种方法;

第一个是,您必须至少拥有 RSpec 版本 3.1.0。

第二个是,你必须添加 def supports_block_expectations?;真的;结束到 RSpec::Matchers::BuiltIn::Compound 类,可以通过猴子修补它或直接编辑 gem 的本地副本。重要说明:这种方式并不完全等同于第一种方式,expect {} 块以这种方式运行多次!

可以在 here 中找到添加了对复合匹配器功能的完全支持的拉取请求。


关注公众号,不定期副业成功案例分享
关注公众号

不定期副业成功案例分享

领先一步获取最新的外包任务吗?

立即订阅