Rspc & stubbing out chained methods

I’ve just started reading up on Rspec and am trying to write a few specs
to test methods in my models.
Consider the following
class Users
has_many :items

def state_of_last_item?
case #uses a named scope
when 0
# do something
when 1
# do something
when 2
# do something

I want to write the spec to test that the ‘do somethings’ are working as
expected. How do I stub out the bit with the named scope? I thought it
would be something like:
but this doesn’t work. What is the correct way to stub out something
like this?

You need a stub for each link in the chain, so to speak. You can do it

@user = stub(“user”, :items => stub(“items”, :active => stub(“active”,
=> stub(“last”, :state => 0))))

or you can create stubs first and then use them together.

last = stub(“last”, :state => 0)
active = stub(“active”, :last => last)
items = stub(“items”, :active => active)
@user = stub(“user”, :items => items)


Thanks for that.
I was hoping that there would be a way to do it without stubbing out
each link.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs