如何比较RSpec中的两个哈希值?

时间:2022-08-31 23:05:39

I have two hashes h1 and h2 that I'd like to compare in RSpec.

我有两个哈希h1和h2,我想在RSpec中进行比较。

I want to check that the elements of h1 are the same as h2 after some transformation, which we'll call f. That is, I want to verify that for every key k in h1, h1[k] == f(h2[k]).

我想检查一些转换后h1的元素与h2相同,我们称之为f。也就是说,我想验证对于h1中的每个密钥k,h1 [k] == f(h2 [k])。

For example, if all the values in h2 are twice as big as the corresponding values in h1, then I want to check that for every key k in h1, h2[k] == h1[k] * 2.

例如,如果h2中的所有值都是h1中相应值的两倍,那么我想检查h1中的每个键k,h2 [k] == h1 [k] * 2。

What's the right way to do this in RSpec? Currently I do:

在RSpec中这样做的正确方法是什么?目前我这样做:

h1 = ...
expect(
  h2.all? { |k,v|
    v == f(h1[k])
  }
).to be true

but that seems clunky.

但这似乎很笨拙。

4 个解决方案

#1


5  

Sounds like what you are testing is the transformation. I would consider writing something like this:

听起来你正在测试的是转型。我会考虑写这样的东西:

it "transforming something does something" do
  base_data = { k1: 1, k2: 2 }
  transformed_data = base_data.each_with_object({}) { |(k, v), t|
    t[k] = f(v)
  }

  expect(transformed_data).to eq(
    k1: 2,
    k2: 4,
  )
end

To me the description clearly states what we are expecting. Then I can easily see from the test what the input is and the expected output. Also, this leverages the hash matcher which will provide a nice diff of the two hashes on a failure:

对我来说,描述清楚地说明了我们的期望。然后我可以很容易地从测试中看到输入是什么和预期的输出。此外,这利用了哈希匹配器,它将在失败时为两个哈希提供一个很好的差异:

expected: {:k1=>2, :k2=>4}
     got: {:k1=>1, :k2=>4}

(compared using ==)

Diff:
@@ -1,3 +1,3 @@
-:k1 => 2,
+:k1 => 1,
 :k2 => 4,

Though I would question what the key-value relationship means. Are these simply test cases you are trying to run through? If so, I'd just make each a unique tests. If there is something more to it, then I may question why the transform method isn't provided the hash to start with.

虽然我会质疑键值关系的含义。这些只是您试图通过的测试用例吗?如果是这样,我会让每个人都进行独特的测试。如果还有更多内容,那么我可能会质疑为什么变换方法没有提供哈希开始。

#2


1  

h1.each do |k, v|
  expect(v).to eq(f(h2[k]))
end

As for me, it seems more readable.

至于我,它似乎更具可读性。

#3


1  

How about:

h2 = f(h1)

expect(h2.keys).to eq(h1.keys)  # I assume you want this, too

h1.keys.each do |k|
  expect(h2[k]).to eq(h1[k] * 2) 
end

A little more long-winded, but maybe more readable?

多一点啰嗦,但也许更具可读性?

#4


0  

For an exact equality:

为了完全平等:

expect(h1).to eq h2.map { |k, v| [k, f(v)] }.to_h

#1


5  

Sounds like what you are testing is the transformation. I would consider writing something like this:

听起来你正在测试的是转型。我会考虑写这样的东西:

it "transforming something does something" do
  base_data = { k1: 1, k2: 2 }
  transformed_data = base_data.each_with_object({}) { |(k, v), t|
    t[k] = f(v)
  }

  expect(transformed_data).to eq(
    k1: 2,
    k2: 4,
  )
end

To me the description clearly states what we are expecting. Then I can easily see from the test what the input is and the expected output. Also, this leverages the hash matcher which will provide a nice diff of the two hashes on a failure:

对我来说,描述清楚地说明了我们的期望。然后我可以很容易地从测试中看到输入是什么和预期的输出。此外,这利用了哈希匹配器,它将在失败时为两个哈希提供一个很好的差异:

expected: {:k1=>2, :k2=>4}
     got: {:k1=>1, :k2=>4}

(compared using ==)

Diff:
@@ -1,3 +1,3 @@
-:k1 => 2,
+:k1 => 1,
 :k2 => 4,

Though I would question what the key-value relationship means. Are these simply test cases you are trying to run through? If so, I'd just make each a unique tests. If there is something more to it, then I may question why the transform method isn't provided the hash to start with.

虽然我会质疑键值关系的含义。这些只是您试图通过的测试用例吗?如果是这样,我会让每个人都进行独特的测试。如果还有更多内容,那么我可能会质疑为什么变换方法没有提供哈希开始。

#2


1  

h1.each do |k, v|
  expect(v).to eq(f(h2[k]))
end

As for me, it seems more readable.

至于我,它似乎更具可读性。

#3


1  

How about:

h2 = f(h1)

expect(h2.keys).to eq(h1.keys)  # I assume you want this, too

h1.keys.each do |k|
  expect(h2[k]).to eq(h1[k] * 2) 
end

A little more long-winded, but maybe more readable?

多一点啰嗦,但也许更具可读性?

#4


0  

For an exact equality:

为了完全平等:

expect(h1).to eq h2.map { |k, v| [k, f(v)] }.to_h