Cloud_cal
Cloud_cal

Reputation: 142

How does Ruby Mocha stub a nested hash?

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.

Upvotes: 1

Views: 1124

Answers (1)

sschmeck
sschmeck

Reputation: 7713

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

Upvotes: 0

Related Questions