« Return to Thread: testing behaviour or testing code?

testing behaviour or testing code?

by David Green :: Rate this Message:

Reply to Author | View in Thread

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
end

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'
end

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?

 « Return to Thread: testing behaviour or testing code?