[rspec-users] Testing Ajax Controller Methods

s.ross cwdinfo at gmail.com
Tue May 15 02:30:53 EDT 2007

I have a bunch of little toggler methods in controllers that look  
sorta like this:

   # This method is only intended to toggle the approved attribute  
via Ajax
    def toggle_approved
      if request.xhr?
        post = Post.find(params[:id])
        post.toggle!('approved') if post
      render :nothing => true

There's essentially nothing here to spec other than:

it "should very silently change the state of the 'approved' field"

But AR takes care of that... and because I'm not exposing the  
intermediary post variable as an instance variable, I can't even see  
whether I did anything without going back and querying the database.  
That feels like stuff I shouldn't be doing in a spec. The other side  
of the coin is that a spec should describe the functionality of the  
controller and one component of that functionality is that it can  
toggle the state of the 'approved' field.

So the conundrum is this: Do I rewrite the code to make it more  
verifiable, changing post to @post? Do I actually take before and  
after snapshots of the 'approved' field? Do I trust AR to do the  
right thing and stop obsessing?

Any thoughts?

More information about the rspec-users mailing list