[rspec-users] A spec where interaction-based testing breaks down... (at least for now)

Pat Maddox pergesu at gmail.com
Tue Jan 23 21:24:51 EST 2007


Here are the specs:
context "Finding all the stylesheets available to a company" do
  setup do
    @mock_company = mock("company")
    @mock_company.stub!(:to_param).and_return "1"
    Stylesheet.stub!(:find)
  end

  def do_find
    Stylesheet.find_available_to @mock_company
  end

  specify "should convert the company into a parameter" do
    @mock_company.should_receive(:to_param).and_return "1"
    do_find
  end

  specify "should find stylesheets belonging to only the company or to
nobody" do
    Stylesheet.should_receive(:find).with(:all, :conditions =>
["company_id IS NULL OR company_id=?", "1"])
    do_find
  end
end

And here's the code:
class Stylesheet < ActiveRecord::Base
  def self.find_available_to(company)
    find :all, :conditions => ["company_id IS NULL OR company_id=?",
company.to_param]
  end
end

You can see that the implementation is duplicated.  In fact, I wrote
the implementation in the spec and then basically just copied it over.

That bugs me.

The obvious way to fix it would be to create a couple stylesheet
entries in the database, run the query, and see if it matches the
expected results.

The only benefit of the current approach (that I can see) is that the
behavior is explicit.  If you've just got the results, you have to
mentally link them to the query.

Anyway I'm just sort of stuck on this one.  I think there's a much
better way that is evading me.

Pat


More information about the rspec-users mailing list