[rspec-users] Shoulda

Jonathan Leighton j at jonathanleighton.com
Thu Jan 10 19:29:38 EST 2008

On Thu, 2008-01-10 at 18:47 -0500, Josh Knowles wrote:
> On 1/10/08, Nathan Sutton <nathan.sutton at gmail.com> wrote:
> > Hmm, that includes a good number of them, but there's still the
> > restful resource to think about, which is in my opinion the most
> > valuable one.  Would you consider the addition of a restful resource
> > matcher similar to shoulda's?
> Yes.  If you work something up I'd happily add it.  Unfortunately I
> don't have the need/time/desire to do it myself right now though.

My reservation with the idea of "should be restful" is that you have to
assume an awful lot about how the controller is implemented. That's fine
if you use scaffolding excessively but if you actually write your own
code (!!) things quickly start to deviate from the trodden path.

Recently I have been writing my controller specs a bit like this (I have
some support code to enable it):

describe PostsController do
  controller_name :posts

  describe "when a post is viewed and the current user is an admin" do
    log_in :as => :admin
    get :show, :id => 42

  describe "when a post is edited by a normal user" do
    log_in :as => :prole
    get :edit, :id => 23
    it_should_redirect_to "the post's page", :at => "post_path(@post)"

Some of the above is me using my creative license but you get the idea.
Just thought it might spark some ideas/opinions... it's certainly not a
perfect implementation/API but I've found the general idea quite useful.
I personally think this is the right level at which to make the
abstraction - you are still specifying the behaviour explicitly, just
writing less code when doing it.


Jonathan Leighton

More information about the rspec-users mailing list