Fighting the Spec Bloat

Every time i run rspec_scaffold and look at the generated specs im shocked anew, there is just so much bla bla and so little information. I know that as a rule of thumb there should be one assertion per test, but in my opinion this is going too far. Note to self: New rule of thumb: “Test one aspect per test”. Meaning that i test ‘database interaction’ and ‘output’ (renders x + assigns y + flash z) in different tests. And when there is only simple logic (1-liner) in either of them, they may be merged.

OLD

before(:each) do
  @address = mock_model(Address)
  Address.stub!(:find).and_return([@address])
end

def do_get
  get :index
end

it "should be successful" do
  do_get
  response.should be_success
end

it "should render index template" do
  do_get
  response.should render_template('index')
end

it "should find all addresses" do
  Address.should_receive(:find).with(:all).and_return([@address])
  do_get
end

it "should assign the found addresses for the view" do
  do_get
  assigns[:addresses].should == [@address]
end

NEW

it "should render index and assign all addresses" do
  Address.expects(:find).with(:all).once.returns([@address])
  
  get :index
  
  response.should render_template('index')
  assigns[:addresses].should equal([@address])
end

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s