【问题标题】:minitest - mock - expect keyword argumentsminitest - 模拟 - 期望关键字参数
【发布时间】:2017-04-19 11:35:17
【问题描述】:

当我想验证 mock 是否发送预期参数时,我可以这样做

@mock.expect(:fnc, nil, ["a, "b"])

但是,如果我想模拟的类看起来像这样

class Foo
    def fnc a:, b:
    end
end

如何模拟它并验证作为a:b: 传递的值?

【问题讨论】:

    标签: ruby mocking minitest


    【解决方案1】:

    以下是我公司代码库中的一个真实示例:

    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
    

    【讨论】:

    • 调用send而不是直接调用方法是违反最小惊讶原则的。期待某人(可能是您)提出“为什么会这样”的问题,他们稍后会在生产代码中阅读该问题。附言如果您真的想这样做,请考虑使用public_send
    • 我也很困惑,认为发生了一些元魔术,但想象一下这个方法真的被称为deliver而不是send,这个例子变得直截了当:你只是在嘲笑deliver 方法,并传递一个块来验证期望,而不是让模拟对象使用其默认实现来验证它。
    【解决方案2】:
    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]
    

    【讨论】:

      【解决方案3】:

      基于@nus comment

      class FooTest
        def test_fnc_arguments
          Foo.new.fnc a: "a", b: "b"
          # assert true # optional
        end
      end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-22
        • 1970-01-01
        • 2019-11-08
        • 2016-02-08
        • 2017-04-01
        相关资源
        最近更新 更多