Pat Maddox
2007-Jan-24 02:24 UTC
[rspec-users] A spec where interaction-based testing breaks down... (at least for now)
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
aslak hellesoy
2007-Jan-24 21:47 UTC
[rspec-users] A spec where interaction-based testing breaks down... (at least for now)
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" > 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. >I wouldn''t use mocks for model specs. I''d test it against the real database. Aslak> Pat > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Martin Emde
2007-Jan-25 20:02 UTC
[rspec-users] A spec where interaction-based testing breaks down... (at least for now)
On 1/24/07, aslak hellesoy <aslak.hellesoy at gmail.com> wrote:> > 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" > > 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. > > > > I wouldn''t use mocks for model specs. I''d test it against the real > database. > > AslakIn addition to actually finding real records from the database for the Stylesheet model (don''t stub find), I would also suggest that to_param is not the right method to use in this case. You''re stating that company_id should equal the url formatted string for a company (which it is typical to overload to handle things like 10-name_of_company). Clearly your database naming says company.id so it''s safer to just use company.id. -Martin -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20070125/32558d34/attachment.html