当我想验证mock是否发送预期参数时,我可以
@mock.expect(:fnc, nil, ["a, "b"])
但是,如果我想要模拟的类看起来像这样
class Foo
def fnc a:, b:
end
end
如何模拟它并验证以a:
,b:
传递的值?
答案 0 :(得分:0)
以下是我公司代码库中的一个真实示例:
mailer = MiniTest::Mock.new
mailer.expect :send, 123 do |template_name:, data:, to:, subject:|
true
end
mailer.send template_name: "xxx", data: {}, to: [], subject: "yyy"
如果您还想验证参数'类型:
mailer.expect :send, 123 do |template_name:, data:, to:, subject:|
template_name.is_a?(String) &&
data.is_a?(Hash) &&
to.is_a?(Array) &&
subject.is_a?(String) &&
end
答案 1 :(得分:0)
require 'minitest/autorun'
class APIClient
def call; end
end
class APIClientTest < Minitest::Test
def test_keyword_aguments_expection
api_client = Minitest::Mock.new
api_client.expect(:call, true, [{ endpoint_url: 'https://api.test', secret_key: 'test' }])
api_client.call(endpoint_url: 'https://api.test', secret_key: 'test')
api_client.verify
end
end
# Running:
.
Finished in 0.000726s, 1377.5945 runs/s, 0.0000 assertions/s.
1 runs, 0 assertions, 0 failures, 0 errors, 0 skips
[Finished in 0.7s]
答案 2 :(得分:-1)
基于@nus comment,
class FooTest
def test_fnc_arguments
Foo.new.fnc a: "a", b: "b"
# assert true # optional
end
end