rspec 模拟:验证其中的期望“应该”方法?

发布于 2024-08-07 21:45:54 字数 438 浏览 6 评论 0原文

我正在尝试使用 rspec 的模拟来设置我可以在“应该”方法中验证的期望...但我不知道如何做到这一点...当我在模拟上调用 .should_receive 方法时,它before:all 方法退出后立即验证预期的调用。

这是一个小例子:

describe Foo, "when doing something" do
 before :all do
  Bar.should_recieve(:baz)
  foo = Foo.new
  foo.create_a_Bar_and_call_baz
 end

 it "should call the bar method" do
  # ??? what do i do here?
 end
end

我如何验证“它“应该””方法中的预期调用?我需要使用 mocha 或其他模拟框架而不是 rspec 的吗?或者 ???

I'm trying to use rspec's mocking to setup expectations that I can verify in the it "should" methods... but I don't know how to do this... when i call the .should_receive methods on the mock, it verifies the expected call as soon as the before :all method exits.

here's a small example:

describe Foo, "when doing something" do
 before :all do
  Bar.should_recieve(:baz)
  foo = Foo.new
  foo.create_a_Bar_and_call_baz
 end

 it "should call the bar method" do
  # ??? what do i do here?
 end
end

How can i verify the expected call in the 'it "should"' method? do i need to use mocha or another mocking framework instead of rspec's? or ???

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

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

发布评论

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

评论(5

凉世弥音 2024-08-14 21:45:54

我将对此进行另一次打击,因为从最初的一组答案和回复中可以清楚地看出,您对要实现的目标存在一些困惑。让我知道这是否更接近您想要做的事情。

describe Foo, "when frobbed" do
  before :all do
    @it = Foo.new

    # Making @bar a null object tells it to ignore methods we haven't 
    # explicitly stubbed or set expectations on
    @bar = stub("A Bar").as_null_object
    Bar.stub!(:new).and_return(@bar)
  end

  after :each do
    @it.frob!
  end

  it "should zap a Bar" do
    @bar.should_receive(:zap!)
  end

  it "should also frotz the Bar" do
    @bar.should_receive(:frotz!)
  end
end

顺便说一句,虽然它有效,但我不太喜欢 Bar.stub!(:new) 模式;我通常更喜欢通过可选参数传递协作者,例如 @it.frob!(@bar)。当没有给出显式参数时(例如在生产代码中),可以默认协作者:def frob!(bar=Bar.new)。这使得测试较少受内部实现的束缚。

I'm going to take another whack at this, because it's clear from the initial set of answers and responses that there was some confusion about what you are trying to accomplish. Let me know if this is closer to what you are trying to do.

describe Foo, "when frobbed" do
  before :all do
    @it = Foo.new

    # Making @bar a null object tells it to ignore methods we haven't 
    # explicitly stubbed or set expectations on
    @bar = stub("A Bar").as_null_object
    Bar.stub!(:new).and_return(@bar)
  end

  after :each do
    @it.frob!
  end

  it "should zap a Bar" do
    @bar.should_receive(:zap!)
  end

  it "should also frotz the Bar" do
    @bar.should_receive(:frotz!)
  end
end

Incidentally, although it works I'm not a big fan of the Bar.stub!(:new) pattern; I usually prefer to pass collaborators in via optional arguments, e.g. @it.frob!(@bar). When not given an explicit argument (e.g. in the production code), the collaborator can be defaulted: def frob!(bar=Bar.new). This keeps the tests a little less bound to internal implementation.

梦醒时光 2024-08-14 21:45:54

作为一般规则,您永远不应该将期望放在 before 块中。 before 块用于设置在多个示例(it 块)之间共享的状态。将期望放在示例本身中。例如:

describe Foo, "when doing something" do
  before :all do
   @foo = Foo.new
  end

  it "should call the bar method" do
    Bar.should_recieve(:baz)
    @foo.create_a_Bar_and_call_baz
  end
end

我通常尝试让我的describe块描述特定的状态(例如describe Car,“给定一整罐汽油”),而不是描述一个动作。

As a general rule you should never put an expectation in a before block. before blocks are for setting up state that is shared across several examples (it blocks). Put the expectation in the example itself. E.g.:

describe Foo, "when doing something" do
  before :all do
   @foo = Foo.new
  end

  it "should call the bar method" do
    Bar.should_recieve(:baz)
    @foo.create_a_Bar_and_call_baz
  end
end

I generally try to have my describe block describe a particular state (e.g. describe Car, "given a full tank of gas"), rather than describing an action.

笑饮青盏花 2024-08-14 21:45:54

should_receive 方法用于设置模拟对象以在调用该方法时返回特定的内容。下面是一个示例:

Bar.should_recieve(:baz).with.({:arg1 => 'this is arg1', :arg2 => 'this is arg2'}).and_return(true)

通常,使用 BDD,您希望测试应用程序的行为。测试调用哪些方法来构建正确的行为是无关紧要的。如果有一天您决定删除 baz 方法,即使您的应用程序的行为没有改变,您也必须更新您的测试。

我认为你试图以一种不应该这样的方式使用 should_receive 。

The should_receive method is used to setup your mock object to return something specific when the method is called. Here's an example:

Bar.should_recieve(:baz).with.({:arg1 => 'this is arg1', :arg2 => 'this is arg2'}).and_return(true)

Typically, with BDD, you want to test the behavior of your app. Testing which methods were called in order to build the proper behavior is irrelevant. If one day you decide to remove the baz method, you'll have to update your tests even though the behavior of your app won't have changed.

I think you're trying to use should_receive in a way that it wasn't meant to work like.

定格我的天空 2024-08-14 21:45:54

我知道这是一个古老的对话,但以防万一有人仍然需要正确的答案,在这里我写了一个关于如何验证 rpec 模拟观察的示例:

require 'spec'
require 'spec/mocks'
include Spec::Mocks::ExampleMethods

o = mock('object')
o.should_receive(:respond_to?).once

space = Spec::Mocks::Space.new
space.add o

# here we should invoke methods, o.respond_to?:foo for instance

space.verify_all

干杯

I know this is an old conversation but just in case somebody still needed a right answer, here I write an example on how to validate rpec mocks spectations:

require 'spec'
require 'spec/mocks'
include Spec::Mocks::ExampleMethods

o = mock('object')
o.should_receive(:respond_to?).once

space = Spec::Mocks::Space.new
space.add o

# here we should invoke methods, o.respond_to?:foo for instance

space.verify_all

cheers

梦开始←不甜 2024-08-14 21:45:54

也许我看得太简单了,但是,您是否需要多次验证与 Bar 的交互?如果是这样,好吧,但我不确定。换句话说,您是否混合了上下文?

如果不是,那么它并不是真正的上下文,而是观察的一部分,这将使它自然地属于 it 块。

Maybe I'm looking at it simplistically, but, do you need to verify that interaction with Bar more than once? If so, ok, but I'm not sure. In other words, are you mixing contexts?

If not, then it's not really context so much as it's part of the observation, which would make it naturally belong to the it block.

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