【问题标题】:rspec test that a method's input is of a certain structurerspec 测试方法的输入是否具有某种结构
【发布时间】:2014-04-11 14:55:55
【问题描述】:

我有一个获取输入的方法。

module MyModule

extend self
    def my_method(a)
      puts a
    end

end

我想确保该方法接收到带有键 :a, :b and :c 的哈希

MyModule.should_receive(:method).with(A_HASH_WITH_KEYS(:a,:b,:c))
calling the method 

【问题讨论】:

    标签: ruby-on-rails ruby unit-testing rspec


    【解决方案1】:

    您可以将hash_includinganyhting 结合使用:

    MyModule.should_receive(:method).with(
      hash_including(a: anything,b: anything,c: anything))
    

    编辑
    似乎hash_including API 的newer versions 接受密钥数组,所以这也应该有效:

    MyModule.should_receive(:method).with(hash_including(:a, :b, :c))
    

    【讨论】:

    • 谢谢,你知道如何验证非空哈希吗?
    • 试试MyModule.should_not_receive(:method).with({})
    • 谢谢,我不是这个意思。 nvm,我会打开另一个问题。
    猜你喜欢
    • 1970-01-01
    • 2013-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-30
    • 2020-07-23
    • 2022-07-06
    相关资源
    最近更新 更多