删除expect_any_instance_of RSpec

时间:2015-10-13 19:41:33

标签: ruby-on-rails ruby rspec

我有一个特定类方法的存根,它对我的​​大多数测试都有效。我在前一个块中设置了模拟以干我的测试。我想删除一个无效的测试存根。我如何使用RSpec做到这一点?

代码

before :all do
 expect_any_instance_of(Foo).to receive(:callback)
end

it 'does a callback on this test' do
 Foo.new
end

it 'does it in this too!' do
 Foo.new.other
end

it 'doesnt do it in this one' do
 # how do i remove the stub???
end

2 个答案:

答案 0 :(得分:1)

您可以使用不同的context,以便您可以像以下一样描述1种行为:

describe 'something' do
  context 'valid with callback' do
    before :each do
      expect_any_instance_of(Foo).to receive(:callback)
    end

    it 'does a callback on this test' do
      Foo.new
    end

    it 'does it in this too!' do
      Foo.new.other
    end
  end

  context 'invalid with callback' do
    it 'doesnt do it in this one' do
      # how do i remove the stub???
    end
  end
end

在上面的代码中,before :each块只是valid with callback上下文的本地,所以你可以在那里测试那个孤立的行为。

答案 1 :(得分:0)

更新: expect_any_instance_of(Foo).to receive(:bar).and_call_original

但是,如果你嘲笑一个类的每个实例,可以认为这可能是一种气味。可能有更清洁的替代方案来构建您的测试。

相关问题