[rspec-users] Ability to stub a method differently the second time its called

David Chelimsky dchelimsky at gmail.com
Wed Jan 26 10:29:48 EST 2011

On Jan 26, 2011, at 7:34 AM, Rob Aldred wrote:

> I'm having a bit of trouble stubbing out a method on a model which has some quite specific behaviour.
> Basically I want to check the method returns something different after ive called update_attributes
> eg.
> @exam.draft?  (returns true)
> @exam.update_attributes(params[:exam])  # sets draft to false
> @exam.draft?  (returns false)

You're testing the framework here. Why?

> To give this some context, if the exam is a draft and update_attributes sets draft to false
> then we want to redirect to the show view instead of edit because only exams that are draft can be edited.
> if @exam.draft?
> 	@exam.update_attributes param[:exam]
> end
> if @exam.draft?
> 	redirect_to :action => :edit
> else
> 	redirect_to :action => :show
> end
> I can obviously set consecutive values with and_return(true,false)
> but this feels a bit unreliable? maybe im just confusing myself

My first question is why the update_attributes needs to be conditional? Couldn't you just do:

@exam.update_attributes param[:exam]

if @exam.draft?
	redirect_to :action => :edit
	redirect_to :action => :show

If it must be the way you have it, I wouldn't do this with stubs on the @exam itself. I'd do what you need to do to set the exam in a draft state (Factory(:exam, :draft => true)???), and then invoke the controller action with :draft => true in one example, and :draft => false in the other.

That make sense?

More information about the rspec-users mailing list