[rspec-users] Controller Spec & DataMapper Chaining

Ken Chien ken.chien at gmail.com
Fri Mar 18 14:01:33 EDT 2011


When calling get/post/etc, the second optional parameter is a hash
representing the "params" hash.

So, in order to test the conditional(s) in your controller, you should
write:
   get :index, :alpha => true  #test the first conditional

In another example, pass in :beta => true like this:
    get :index, :beta => true

HTH,
Ken


2011/3/18 Sid Wood <sidwood2 at gmail.com>

> Thanks for the replies guys.
>
> My problem is writing a test for
>
> @widgets = @widgets.by_alpha(params[:alpha])
>
> DataMapper allows you to chain additional criteria on an existing query and
> doesnt execute that query until in is used in the view. So the above
> statement gets baked into the previous default query @widgets =
> Widget.all(:order => [ :name.asc ]).
>
> I'd just like to write a test that confirms the controller is in fact
> calling @widgets.by_alpha(params[:alpha]) when the params[:alpha] is
> present.
>
> Here is the code again so you don't have to scroll to see it
>
>
> def index
>   @widgets = Widget.all(:order => [ :name.asc ])
>   if params[:alpha]
>     @widgets = @widgets.by_alpha(params[:alpha])
>   elsif params[:beta]
>     @widgets = @widgets.by_beta(params[:beta])
>   end
> end
>
> Just to recap, this code is valid and works, I just don't know how to write
> a test for it.
>
> Cheers,
>
> Sid
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110318/dfa6aba0/attachment.html>


More information about the rspec-users mailing list