问题
I have a model Family with a method location
which merges the location
outputs of other objects, Members. (Members are associated with families, but that's not important here.)
For example, given
- member_1 has
location
== 'San Diego (traveling, returns 15 May)' - member_2 has
location
== 'San Diego'
Family.location might return 'San Diego (member_1 traveling, returns 15 May)' The specifics are unimportant.
To simplify the testing of Family.location, I want to stub Member.location. However, I need it to return two different (specified) values as in the example above. Ideally, these would be based on an attribute of member
, but simply returning different values in a sequence would be OK. Is there a way to do this in RSpec?
It's possible to override the Member.location method within each test example, such as
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
but I doubt this is good practice. In any case, when RSpec is running a series of examples it doesn't restore the original class, so this kind of override "poisons" subsequent examples.
So, is there a way to have a stubbed method return different, specified values on each call?
回答1:
You can stub a method to return different values each time it's called;
allow(@family).to receive(:location).and_return('first', 'second', 'other')
So the first time you call @family.location
it will return 'first', the second time it will return 'second', and all subsequent times you call it, it will return 'other'.
回答2:
RSpec 3 syntax:
allow(@family).to receive(:location).and_return("abcdefg", "bcdefgh")
回答3:
The accepted solution should only be used if you have a specific number of calls and need a specific sequence of data. But what if you don't know the number of calls that will be made, or don't care about the order of data only that it's something different each time? As OP said:
simply returning different values in a sequence would be OK
The issue with and_return
is that the return value is memoized. Meaning even if you'd return something dynamic you'll always get the same.
E.g.
allow(mock).to receive(:method).and_return(SecureRandom.hex)
mock.method # => 7c01419e102238c6c1bd6cc5a1e25e1b
mock.method # => 7c01419e102238c6c1bd6cc5a1e25e1b
Or a practical example would be using factories and getting the same IDs:
allow(Person).to receive(:create).and_return(build_stubbed(:person))
Person.create # => Person(id: 1)
Person.create # => Person(id: 1)
In these cases you can stub the method body to have the code executed every time:
allow(Member).to receive(:location) do
{ residence: Faker::Address.city }
end
Member.location # => { residence: 'New York' }
Member.location # => { residence: 'Budapest' }
Note that you have no access to the Member object via self
in this context but can use variables from the testing context.
E.g.
member = build(:member)
allow(member).to receive(:location) do
{ residence: Faker::Address.city, work: member.male? 'his_work' : 'her_work' }
end
回答4:
If for some reason you want to use the old syntax, you can still:
@family.stub(:location).and_return('foo', 'bar')
回答5:
I've tried the solution outline here above but it does not work for my. I solved the problem by stubbing with a substitute implementation.
Something like:
@family.stub(:location) { rand.to_s }
来源:https://stackoverflow.com/questions/5947999/can-rspec-stubbed-method-return-different-values-in-sequence