Ruby Mocha如何对嵌套哈希进行存根处理? [英] How does Ruby Mocha stub a nested hash?
本文介绍了Ruby Mocha如何对嵌套哈希进行存根处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我这里有一个需要使用Mocha进行模拟的方法,但是目前我不知道如何在这里模拟嵌套的哈希.
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']
到目前为止,我知道如何对Products.new(:A => "aa", :B => "bb")
进行存根处理,但是对后面的哈希部分一无所知.
So far, i know how to stub Products.new(:A => "aa", :B => "bb")
, but have no idea with the hash part after it.
谢谢.
推荐答案
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
这篇关于Ruby Mocha如何对嵌套哈希进行存根处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文