[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
  stub_resource

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

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

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.

Jon

-- 
Jonathan Leighton
http://jonathanleighton.com/



More information about the rspec-users mailing list