将规范隔离到方法的一个特定调用

时间:2011-10-22 23:43:23

标签: ruby rspec isolation

我正在编写一个对象(Sample)的规范,它会多次调用另一个对象的方法(IO#delete)。

我想隔离这种方法的测试,但是当我这样做时:

class Sample
  def delete_them
    io.delete "file1"
    io.delete "folder1"
  end
end

describe Sample do
  let(:io) { stub.as_null_object }
  subject  { Sample.new.tap { |s| s.stub(:io).and_return(io) }}

  it "deletes file1" do
    io.should_receive(:delete).with("file1")
    subject.delete_them
  end

  it "deletes folder1" do
    io.should_receive(:delete).with("folder1")
    subject.delete_them
  end
end

如果我调用多个方法,那不是问题,因为我使用的是空对象模式。但是,在这种情况下,当我执行第二次测试时,它会抱怨:

1) Sample instance methods#delete_them deletes folder1
   Failure/Error: io.should_receive(:delete).with("folder1")
     Stub received :delete with unexpected arguments
       expected: ("folder1")
            got: ("file1")

有没有办法表明所有的电话都必须被忽略除了我想要确定的那个?

1 个答案:

答案 0 :(得分:0)

此代码编译良好。这是造成我问题的另一个问题。