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

发布于 2024-12-11 07:15:34 字数 883 浏览 0 评论 0原文

我正在为一个对象(示例)编写一个规范,该对象多次调用另一个对象的方法(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")

有没有一种方法可以指示必须忽略所有调用,除了我试图确保正在完成的调用之外?

I am writing a spec for an object (Sample) that calls another object's method (IO#delete) a number of time.

I want to isolate the tests of this method, however when I do this:

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

If I call multiple methods it's not a problem because I am using the null object pattern. However, in this case when I execute the second test, it complains:

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")

Is there a way to indicate that all the calls must be ignored except the one I am trying to make sure is being done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

中二柚 2024-12-18 07:15:34

这段代码编译得很好。这是导致我的问题的另一个问题。

This code compiles fine. It was another issue that was causing my problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文