【问题标题】:How to use custom RR wildcard matcher?如何使用自定义 RR 通配符匹配器?
【发布时间】:2012-11-05 00:34:08
【问题描述】:

我为RR 创建了一个通配符匹配器,它通过将 JSON 字符串解析为哈希来匹配它们。这是因为 JSON(反)序列化不保留顺序;如果我们有:

{ 'foo': 42, 'bar': 123 }

...然后在(反)序列化之后,我们可能会发现我们的更新方法是这样调用的:

{ 'bar': 123, 'foo': 42 }

通配符匹配器如下所示:

class RR::WildcardMatchers::MatchesJsonString

  attr_reader :expected_json_hash

  def initialize(expected_json_string)
    @expected_json_hash = JSON.parse(expected_json_string)
  end

  def ==(other)
    other.respond_to?(:expected_json_hash) && other.expected_json_hash == self.expected_json_hash
  end

  def wildcard_matches?(actual_json_string)
    actual_json_hash = JSON.parse(actual_json_string)
    @expected_json_hash == actual_json_hash
  end
end

module RR::Adapters::RRMethods
  def matches_json(expected_json_string)
    RR::WildcardMatchers::MatchesJsonString.new(expected_json_string)
  end
end

...我们像这样使用它:

describe 'saving manifests' do

  before do
    @manifests = [
      { :sections => [], 'title' => 'manifest1' },
      { :sections => [], 'title' => 'manifest2' }
    ]

    mock(manifest).create_or_update!(matches_json(@manifests[0].to_json)) { raise 'uh oh' }
    mock(manifest).create_or_update!(matches_json(@manifests[1].to_json))

    parser = ContentPack::ContentPackParser.new({
                                                  'manifests' => @manifests
                                                })
    @errors = parser.save
  end

  it 'updates manifests' do
    manifest.should have_received.create_or_update!(anything).twice
  end
end

这符合RR documentation。然而,mock() 期望的参数与 JSON 匹配,而不是期望参数是 MatchesJsonString 对象:

 1) ContentPack::ContentPackParser saving manifests updates manifests
     Failure/Error: mock(Manifest).create_or_update!(matches_json(@manifests[0].to_json)) { raise 'uh oh' }
     RR::Errors::TimesCalledError:
       create_or_update!(#<RR::WildcardMatchers::MatchesJsonString:0x13540def0 @expected_json_hash={"title"=>"manifest1", "sections"=>[]}>)
       Called 0 times.
       Expected 1 times.
     # ./spec/models/content_pack/content_pack_parser_spec.rb:196

【问题讨论】:

  • 在发帖时,我注意到总共有 15 个问题标记为“rr”。我怀疑第一个答案可能是“不要使用 rr”;-)

标签: ruby rspec rr


【解决方案1】:

答案是我链接的文档中有错字。这(我的重点):

#wildcard_matches?(other)

wildcard_matches? is the method that actually checks the argument against the expectation. It should return true if other is considered to match, false otherwise. In the case of DivisibleBy, wildcard_matches? reads:

...实际上应该是:

#wildcard_match?(other)

...

我的一位同事建议我们将我们的代码与 rr gem 中定义的匹配器之一进行比较,然后发现差异。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-01
    • 2014-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-13
    • 2020-07-07
    相关资源
    最近更新 更多