确切的论点与 Minitest 模拟期望相匹配?
Exact argument matches with Minitest mock expectations?
我正在编写 Minitest 单元测试,需要检查被测代码是否从 HTTP 响应模拟中提取了特定的哈希值。所以我设定了以下期望:
@http_response.expect :[], 3141, [ "X-Pagination-TotalElements" ]
@http_response.expect :[], 32, [ "X-Pagination-TotalPages" ]
然而匹配似乎是基于大小写相等的,第二个期望覆盖第一个。
完成我想做的事情的正确方法是什么?
However it seems that matching is based on case equality [===]
...对于字符串 class 与 ==
相同。
the second expectation overrides the first.
看来 expect statements
的顺序对于 MiniTest::Mock
个对象很重要:
require 'minitest/autorun'
resp = MiniTest::Mock.new
resp.expect :[], 3141, ["X-Pagination-TotalElements"]
resp.expect :[], 32, ["X-Pagination-TotalPages"]
puts resp["X-Pagination-TotalElements"]
puts resp["X-Pagination-TotalPages"]
--output:--
3141
32
Run options: --seed 29457
# Running:
Finished in 0.001500s, 0.0000 runs/s, 0.0000 assertions/s.
0 runs, 0 assertions, 0 failures, 0 errors, 0 skips
但这失败了:
require 'minitest/autorun'
resp = MiniTest::Mock.new
resp.expect :[], 3141, ["X-Pagination-TotalElements"]
resp.expect :[], 32, ["X-Pagination-TotalPages"]
puts resp["X-Pagination-TotalPages"]
puts resp["X-Pagination-TotalElements"]
--output:--
/Users/7stud/.rvm/gems/ruby-2.1.2/gems/minitest-5.4.0/lib/minitest/mock.rb:148:in
method_missing': mocked method :[] called with unexpected arguments
["X-Pagination-TotalPages"] (MockExpectationError) from 1.rb:7:in
'
并且数量也发挥了作用:
require 'minitest/autorun'
resp = MiniTest::Mock.new
resp.expect :[], 3141, ["X-Pagination-TotalElements"]
puts resp["X-Pagination-TotalElements"]
puts resp["X-Pagination-TotalElements"]
--output:--
3141
/Users/7stud/.rvm/gems/ruby-2.1.2/gems/minitest-5.4.0/lib/minitest/mock.rb:122:in
method_missing': No more expects available for :[]:
["X-Pagination-TotalElements"] (MockExpectationError) from 1.rb:7:in
'
结果,意思是:
resp.expect :[], 3141, ["X-Pagination-TotalElements"]
resp.expect :[], 32, ["X-Pagination-TotalPages"]
..是:
我希望最初使用键 "X-Pagination-TotalElements" 调用 resp[]
将 return 3141。
我希望使用密钥 "X-Pagination-TotalPages" 再次调用 resp[]
将 return 32。
我预计任何额外的调用或不按该顺序调用都会导致失败。
...
What is the correct way to accomplish what I am trying to do?
按照将在您的代码中调用的顺序添加 expect 语句。
我正在编写 Minitest 单元测试,需要检查被测代码是否从 HTTP 响应模拟中提取了特定的哈希值。所以我设定了以下期望:
@http_response.expect :[], 3141, [ "X-Pagination-TotalElements" ]
@http_response.expect :[], 32, [ "X-Pagination-TotalPages" ]
然而匹配似乎是基于大小写相等的,第二个期望覆盖第一个。
完成我想做的事情的正确方法是什么?
However it seems that matching is based on case equality [===]
...对于字符串 class 与 ==
相同。
the second expectation overrides the first.
看来 expect statements
的顺序对于 MiniTest::Mock
个对象很重要:
require 'minitest/autorun'
resp = MiniTest::Mock.new
resp.expect :[], 3141, ["X-Pagination-TotalElements"]
resp.expect :[], 32, ["X-Pagination-TotalPages"]
puts resp["X-Pagination-TotalElements"]
puts resp["X-Pagination-TotalPages"]
--output:--
3141
32
Run options: --seed 29457
# Running:
Finished in 0.001500s, 0.0000 runs/s, 0.0000 assertions/s.
0 runs, 0 assertions, 0 failures, 0 errors, 0 skips
但这失败了:
require 'minitest/autorun'
resp = MiniTest::Mock.new
resp.expect :[], 3141, ["X-Pagination-TotalElements"]
resp.expect :[], 32, ["X-Pagination-TotalPages"]
puts resp["X-Pagination-TotalPages"]
puts resp["X-Pagination-TotalElements"]
--output:--
/Users/7stud/.rvm/gems/ruby-2.1.2/gems/minitest-5.4.0/lib/minitest/mock.rb:148:inmethod_missing': mocked method :[] called with unexpected arguments ["X-Pagination-TotalPages"] (MockExpectationError) from 1.rb:7:in
'
并且数量也发挥了作用:
require 'minitest/autorun'
resp = MiniTest::Mock.new
resp.expect :[], 3141, ["X-Pagination-TotalElements"]
puts resp["X-Pagination-TotalElements"]
puts resp["X-Pagination-TotalElements"]
--output:--
3141/Users/7stud/.rvm/gems/ruby-2.1.2/gems/minitest-5.4.0/lib/minitest/mock.rb:122:in
method_missing': No more expects available for :[]: ["X-Pagination-TotalElements"] (MockExpectationError) from 1.rb:7:in
'
结果,意思是:
resp.expect :[], 3141, ["X-Pagination-TotalElements"]
resp.expect :[], 32, ["X-Pagination-TotalPages"]
..是:
我希望最初使用键 "X-Pagination-TotalElements" 调用
resp[]
将 return 3141。我希望使用密钥 "X-Pagination-TotalPages" 再次调用
resp[]
将 return 32。我预计任何额外的调用或不按该顺序调用都会导致失败。
...
What is the correct way to accomplish what I am trying to do?
按照将在您的代码中调用的顺序添加 expect 语句。