How does Ruby Mocha stub a nested hash?

丶灬走出姿态 提交于 2020-03-06 08:38:20

问题


I have a method here that needs to be mocked using Mocha, but currently i have no clue how to mock the nested hash here.

Products.new(:A => "aa", :B => "bb").containers['container_A'].elements['element_b']

So far, i know how to stub Products.new(:A => "aa", :B => "bb"), but have no idea with the hash part after it.

Thanks in advance.


回答1:


What about a hash/OpenStruct?

require 'ostruct'

product.expects(:containers).
        returns('container_A' => OpenStruct.new(:elements => {'element_b' => 'expected_value'}))

puts product.containers['container_A'].elements['element_b']
# => expected_value


来源:https://stackoverflow.com/questions/31440902/how-does-ruby-mocha-stub-a-nested-hash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!