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 items.active.last.state #uses a named scope
when 0
# do something
when 1
# do something
when 2
# do something
end
end
end

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:
@user.stub!(‘items.active.last.state’).and_return(0)
but this doesn’t work. What is the correct way to stub out something
like this?
Thanks
TC

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

@user = stub(“user”, :items => stub(“items”, :active => stub(“active”,
:last
=> 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)

Regards,
Craig

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