开发者

How do you mock an OpenCascade?

OpenCascade is a recursive OpenStruct from Hashery:

http://rubyworks.github.com/hashery/

It allows you to re开发者_高级运维fer to nested values in a hash via a sequence of keys:

h = {:a=>1,:b=>{:x=>1,:y=>2}}
c = OpenCascade.new(h)
c.b.y
=> 2

We're using it to read in a YAML config. Now we'd like to mock the values in tests, however

mock(c.b).y { 5 }

doesn't work. How do we mock it?


When the question was asked the implementation of OpenCascade's method_missing created a new object each time a Hash was queried:

def method_missing(sym, *args, &blk)
  # ...snip..
  if key?(name)
    self[name] = transform_entry(self[name])
  # ...snip...
  end
end

private

#
def transform_entry(entry)
  case entry
  when Hash
    OpenCascade.new(entry) #self.class.new(val)
  when Array
    entry.map{ |e| transform_entry(e) }
  else
    entry
  end
end

This means that in that version the following:

c.b equal? c.b
# => false

That's why mocking c.b didn't work...

It has since been fixed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜