[rspec-users] How much details is enough?

Matt Berther matt at mattberther.com
Fri Apr 18 01:45:43 EDT 2008


Hi all,

Long time lurker, first-time question. :)

I've been going back and forth trying to decide how much detail is  
enough in my controller specs. I feel that if I put too much detail in  
them, then my tests become very brittle and do not allow me to  
refactor my code without significant effort on the tests. After all, I  
have tests so that I can refactor, right?

So, let me show two ways that Ive done controller tests and gain  
insight from my enlightened colleagues on this list to help me make a  
decision about which way to go.

The first is very general coverage that describes the outcome of the  
behavior:


describe FeedsController, 'get /feeds/' do
   before(:each) do
     @request.env["HTTP_ACCEPT"] = "application/xml"
     Model.should_receive(:find).with(any_args).and_return  
mock_model(Model)
   end

   it "should return success" do
     get '/'
     response.should be_success
   end

   it "should return 405 (Method Not Allowed) if HTTP_ACCEPT is text/ 
html" do
     @request.env["HTTP_ACCEPT"] = "text/html"
     get '/'
     response.response_code.should == 405
   end
end

The second one is much more detailed:

describe FeedsController, 'get /feeds/' do
   before(:each) do
     @model = mock_model(Model)
     @request.env["HTTP_ACCEPT"] = "application/xml"
     Model.should_receive(:find).with(any_args).and_return @model
   end

   it "should assign to the model" do
     get '/'
     assigns[:model].should == model
   end

   it "should render feed template" do
     get '/'
     response.should render_template('feeds/model_feed.xml.erb')
   end
end

Obviously, both are very basic in their implementation, but still, I  
ask... If you were writing the specs, which way would you write them?  
Thanks for any guidance.

--
Matt Berther
http://www.mattberther.com






More information about the rspec-users mailing list