RSpec stubbed方法可以按顺序返回不同的值吗?

问题描述 投票:61回答:5

我有一个模型Family,其方法是location,它合并了其他对象的location输出,成员。 (成员与家庭有关,但这在这里并不重要。)

例如,给定

  • member_1有location =='圣地亚哥(旅游,5月15日返回)'
  • member_2有location =='圣地亚哥'

Family.location可能会返回'圣地亚哥(member_1旅行,5月15日返回)'细节不重要。

为了简化Family.location的测试,我想要存根Member.location。但是,我需要它返回两个不同的(指定的)值,如上例所示。理想情况下,这些将基于member的属性,但只是在序列中返回不同的值就可以了。在RSpec有办法做到这一点吗?

可以在每个测试示例中覆盖Member.location方法,例如

it "when residence is the same" do 
  class Member
    def location
      return {:residence=>'Home', :work=>'his_work'} if self.male?
      return {:residence=>'Home', :work=>'her_work'}
    end
  end
  @family.location[:residence].should == 'Home'
end

但我怀疑这是好习惯。在任何情况下,当RSpec运行一系列示例时,它不会恢复原始类,因此这种覆盖“毒药”后续示例。

那么,有没有办法让stubbed方法在每次调用时返回不同的指定值?

ruby-on-rails testing rspec stub
5个回答
129
投票

您可以将方法存根以在每次调用时返回不同的值;

allow(@family).to receive(:location).and_return('first', 'second', 'other')

因此,当您第一次调用@family.location时,它将返回'first',第二次返回'second',以及随后的所有调用它,它将返回'other'。


12
投票

RSpec 3语法:

allow(@family).to receive(:location).and_return("abcdefg", "bcdefgh")

1
投票

如果由于某种原因您想使用旧语法,您仍然可以:

@family.stub(:location).and_return('foo', 'bar')

1
投票

只有在您有特定数量的呼叫且需要特定数据序列时,才应使用已接受的解决方案。但是,如果您不知道将要进行的呼叫数量,或者不关心数据的顺序,那么每次都会有什么不同之处呢?正如OP所说:

只需在序列中返回不同的值即可

and_return的问题是返回值是memoized。这意味着,即使你返回一些动态的东西,你也总会得到同样的东西。

EG

allow(mock).to receive(:method).and_return(SecureRandom.hex)
mock.method # => 7c01419e102238c6c1bd6cc5a1e25e1b
mock.method # => 7c01419e102238c6c1bd6cc5a1e25e1b

或者一个实际的例子是使用工厂并获得相同的ID:

allow(Person).to receive(:create).and_return(build_stubbed(:person))
Person.create # => Person(id: 1)
Person.create # => Person(id: 1)

在这些情况下,您可以存根方法体,以便每次都执行代码:

allow(Member).to receive(:location) do
  { residence: Faker::Address.city }
end
Member.location # => { residence: 'New York' }
Member.location # => { residence: 'Budapest' }

请注意,在此上下文中,您无法通过self访问Member对象,但可以使用测试上下文中的变量。

EG

member = build(:member)
allow(member).to receive(:location) do
  { residence: Faker::Address.city, work: member.male? 'his_work' : 'her_work' }
end

0
投票

我已经尝试过上面的解决方案大纲,但它对我来说不起作用。我通过替换实现来解决问题。

就像是:

@family.stub(:location) { rand.to_s }
© www.soinside.com 2019 - 2024. All rights reserved.