[rspec-users] A spec where interaction-based testing breaks down... (at least for now)
aslak.hellesoy at gmail.com
Wed Jan 24 16:47:49 EST 2007
On 1/24/07, Pat Maddox <pergesu at gmail.com> wrote:
> 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"
> def do_find
> Stylesheet.find_available_to @mock_company
> specify "should convert the company into a parameter" do
> @mock_company.should_receive(:to_param).and_return "1"
> 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"])
> 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=?",
> 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.
I wouldn't use mocks for model specs. I'd test it against the real database.
> rspec-users mailing list
> rspec-users at rubyforge.org
More information about the rspec-users