如何在 rspec 中模拟实例变量

2024-05-09

我有两节课OneClass and AnotherClass:

class OneClass
  def initialize(*args)
    @another_member = AnotherClass.new()
  end

  def my_method()
    if @another_member.another_method1() then
      @another_member.another_method2()
    end
    @another_member.another_method3()
  end
end

我要写单位OneClass。 我该如何嘲笑@another_member?


有了安东尼的想法,我就让它发挥作用。

describe OneClass do
  before(:each) { @one_object = OneClass.new }

  describe 'my_method' do
    it 'should work' do
      mock_member = double
      allow(mock_member).to receive(:another_method1).and_return(true)
      @one_object.instance_variable_set(:@another_member, mock_member)

      @one_object.my_method()

      expect(mock_member).to have_received(:another_method1)
    end
  end
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 rspec 中模拟实例变量 的相关文章

随机推荐