【发布时间】:2015-10-20 11:08:26
【问题描述】:
我想用 RSpec 测试一个函数 my_test。该函数在 rails 4 中调用另一个类的类方法 MyHelper.func。 我在 MyHelper 类中使用 mock 来说明 func,我想将 func 接收到的参数与一些 const 值匹配。
我用过
expect(<double>).to receive(func).with(arguments)
但其中一个 const 参数是包含 2 个以上项目的哈希。当我测试我的 函数,RSpec 抛出错误:
received :func with unexpected arguments.
expected: [{"1"=>33.33},{"2"=>33.33},{"3"=>33.33}]
got: [{"2"=>33.33},{"1"=>33.33},{"3"=>33.33}]
有没有办法在两个数组之间进行匹配? 代码:
my_hash = [{"1"=>33.33},{"2"=>33.33},{"3"=>33.33}]
notifier = class_double("MyHelper").
as_stubbed_const(:transfer_nested_constant => true)
expect(notifier).to receive(:func).with(my_hash)
【问题讨论】:
标签: ruby-on-rails-4 rspec mocking