RSpec“全部更改”匹配器

发布于 2025-01-11 04:53:11 字数 430 浏览 1 评论 0原文

我有一些更新记录的工作,我想要类似的东西:

  it 'updates each record' do
    expect {
      described_class.perform_now
    }.to(change_all_of{
      Record.pluck(:updated_at)
    })
  end

只是我找不到任何看起来像如何完成此操作的东西,我可以识别为如何编写的文档自定义匹配器。

问题是,如果任何元素发生更改,数组上的change将返回true;我只想在每个元素发生变化时返回 true。

任何人都可以指出我错过的任何可以让我这样做的内容,或者我需要编写自己的 change 匹配器的任何文档/信息吗?

I've got some job that updates records, and I want something like:

  it 'updates each record' do
    expect {
      described_class.perform_now
    }.to(change_all_of{
      Record.pluck(:updated_at)
    })
  end

Only I can't find anything that looks like how to accomplish this, or what I can recognize as the docs for how to write a custom matcher.

The issue is that change, on an array, will return true if any element has changed; I only want to return true if every element has changed.

Can anyone point me either at whatever I missed that would let me do this, OR, whatever docs/info I need to write my own change matcher?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡淡の花香 2025-01-18 04:53:11

好吧,感谢这个答案在另一个问题上,并盯着实际代码,这是我的得到了 -

module RSpec
  module Matchers
    def change_all &block
      BuiltIn::ChangeAll.new(nil, nil, &block)
    end

    module BuiltIn
      class ChangeAll < Change
        def initialize receiver=nil, message=nil, &block
          @change_details = ChangeAllDetails.new(receiver, message, &block)
        end

        def failure_message
          "expected all elements to change, but " + (
            @change_details.actual_after & @change_details.actual_before
          ).collect do |unchanged|
            "before[#{@change_details.actual_before.find_index(unchanged)}] " \
            "after[#{@change_details.actual_after.find_index(unchanged)}] " \
            "#{unchanged}"
          end.join(",") + " remained the same"
        end
      end

      class ChangeAllDetails < ChangeDetails
        attr_accessor :actual_before

        def changed?
          !(@actual_after & @actual_before).any?
        end
      end
    end
  end
end

欢迎提出建议!

Alright, thanks to this answer on another question, and staring at the actual code, here's what I've got -

module RSpec
  module Matchers
    def change_all &block
      BuiltIn::ChangeAll.new(nil, nil, &block)
    end

    module BuiltIn
      class ChangeAll < Change
        def initialize receiver=nil, message=nil, &block
          @change_details = ChangeAllDetails.new(receiver, message, &block)
        end

        def failure_message
          "expected all elements to change, but " + (
            @change_details.actual_after & @change_details.actual_before
          ).collect do |unchanged|
            "before[#{@change_details.actual_before.find_index(unchanged)}] " \
            "after[#{@change_details.actual_after.find_index(unchanged)}] " \
            "#{unchanged}"
          end.join(",") + " remained the same"
        end
      end

      class ChangeAllDetails < ChangeDetails
        attr_accessor :actual_before

        def changed?
          !(@actual_after & @actual_before).any?
        end
      end
    end
  end
end

Suggestions welcome!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文