[rspec-users] Testing Ajax Controller Methods

Matthijs Langenberg mlangenberg at gmail.com
Tue May 15 02:55:49 EDT 2007


Mock objects are great for this!

it "should very silently change the state of the 'approved' field" do
  post = mock('post')
  post.should_receive(:toggle!).with('approved')
  Post.should_receive(:find).with("1").and_return(post)
  xhr post :toggle_approved, :id => 1
end

On 5/15/07, s.ross <cwdinfo at gmail.com> wrote:
> 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
>       end
>       render :nothing => true
>     end
>
> 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?
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list