[rspec-users] testing behaviour or testing code?

David Green justnothing at tiscali.co.uk
Fri Aug 24 05:42:17 EDT 2007

hypothetical question for all you BDD experts:

I want to make sure that a :list action always returns widgets in
alphabetical order. There's at least 2 ways of doing this:

it "should fetch items in alphabetical order" do
  Widget.should_receive(:find).with(:order => "name ASC")
  get :list

it "should fetch items in alphabetical order" do
  [:red, :green, :blue].each {|x| Widget.create(:name => x) }
  get :list
  assigns[:widgets].first.name.should == 'blue'
  assigns[:widgets].last.name.should == 'red'

with the first method, I get to mock the important calls and stub the rest,
but the example is very closely tied to the implementation. If I change from
Widget.find to Widget.find_alphabetically then the example breaks (assuming
find_alphabetically() doesn't use AR::Base.find)

with the second method, I'm testing the behaviour more than how it's
implemented. I don't care what the action does as long as it gives me an
array of widgets sorted alphabetically, but I spend more time setting things
up and worrying about model validations. In addition, the specs are tied to
a db.

so which is the better method, and is there another way i havn't considered
that gives me the best of both worlds?

View this message in context: http://www.nabble.com/testing-behaviour-or-testing-code--tf4322619.html#a12309322
Sent from the rspec-users mailing list archive at Nabble.com.

More information about the rspec-users mailing list