<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>opensoul.org - RSpec is getting too intimate with my code Comments</title>
  <id>tag:opensoul.org,2009:/2007/6/20/rspec-is-getting-too-intimate-with-my-code/comments</id>
  <generator uri="http://mephistoblog.com" version="0.8.0">Mephisto Drax</generator>
  <link href="http://opensoul.org/2007/6/20/rspec-is-getting-too-intimate-with-my-code/comments.xml" rel="self" type="application/atom+xml"/>
  <link href="/2007/6/20/rspec-is-getting-too-intimate-with-my-code" rel="alternate" type="text/html"/>
  <updated>2008-05-27T01:33:55Z</updated>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Nolan Eakins</name>
    </author>
    <id>tag:opensoul.org,2007-06-20:3089:6259</id>
    <published>2008-05-27T01:33:55Z</published>
    <updated>2008-05-27T01:33:55Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/6/20/rspec-is-getting-too-intimate-with-my-code" rel="alternate" type="text/html"/>
    <title>Comment on 'RSpec is getting too intimate with my code' by Nolan Eakins</title>
<content type="html">&lt;p&gt;What a small world Google presents! Turned this up while looking for a solution to my &#8220;invalid options&#8221; to #render problem I was having (use RSpec&#8217;s trunk btw).&lt;/p&gt;


	&lt;p&gt;Anyway, I&#8217;ll add my two cents regarding mocks. I&#8217;m starting to think that any method I mock with `should_receive` specifies the contract that the method I&#8217;m testing has with its collaborators.&lt;/p&gt;


	&lt;p&gt;At least to me this makes sense, especially given duck typing. So for example, the spec has something like &#8221;@car.engine.should_receive(:start)&#8221; states that whatever object &#8221;@car.engine&#8221; is, it has to have a &#8220;start&#8221; method.&lt;/p&gt;


	&lt;p&gt;Now when I release libcar and some fool passes a Duck instance as an engine, they have no argument when they complain the car won&#8217;t start. That&#8217;s because Ducks quack, while Engines start. I can tell them to look at the spec to find out if their new &#8220;engine&#8221; will work with Car.&lt;/p&gt;


	&lt;p&gt;And now when I change my implementation to use something like #hotwire instead of #start, my spec will complain and rightly so. I just broke every client to my library that expects the engine to #start!&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Loren Johnson</name>
    </author>
    <id>tag:opensoul.org,2007-06-20:3089:4825</id>
    <published>2007-12-24T05:24:45Z</published>
    <updated>2007-12-24T05:24:45Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/6/20/rspec-is-getting-too-intimate-with-my-code" rel="alternate" type="text/html"/>
    <title>Comment on 'RSpec is getting too intimate with my code' by Loren Johnson</title>
<content type="html">&lt;p&gt;Back to the Fixtures versus mocking part of this debate. . . Adam Williams and John Long recently released this plugin for Rspec which implements the data &#8220;Scenarios&#8221; pattern nicely. We&#8217;re using it as we re-write all the tests in Radiant in Rspec and it&#8217;s making live much better than writing fixtures.&lt;/p&gt;


	&lt;p&gt;http://agilewebdevelopment.com/plugins/scenarios&lt;/p&gt;


	&lt;p&gt;Hope that helps someone,&lt;/p&gt;


	&lt;p&gt;Loren&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Rodrigo Alvarez</name>
    </author>
    <id>tag:opensoul.org,2007-06-20:3089:3519</id>
    <published>2007-08-30T11:56:33Z</published>
    <updated>2007-08-30T11:56:33Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/6/20/rspec-is-getting-too-intimate-with-my-code" rel="alternate" type="text/html"/>
    <title>Comment on 'RSpec is getting too intimate with my code' by Rodrigo Alvarez</title>
<content type="html">&lt;p&gt;I&#8217;m not sure, but I think that I have found a way to test just behaviour in a controller, in isolation and without touching the database. Take a look at this post http://papipo.blogspot.com/2007/08/bdd-isolation-integration.html&lt;/p&gt;


	&lt;p&gt;Thanks.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Zach Moazeni</name>
    </author>
    <id>tag:opensoul.org,2007-06-20:3089:3265</id>
    <published>2007-07-20T00:06:52Z</published>
    <updated>2007-07-20T00:06:52Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/6/20/rspec-is-getting-too-intimate-with-my-code" rel="alternate" type="text/html"/>
    <title>Comment on 'RSpec is getting too intimate with my code' by Zach Moazeni</title>
<content type="html">&lt;blockquote&gt;
Count the number of controller actions in the latest Rails app that you’ve worked on that doesn’t touch the model. (0 for the one I’m working on right now)
&lt;/blockquote&gt;

	&lt;p&gt;I didn&#8217;t said that the controller shouldn&#8217;t interact with the models. In fact, a Resource-Oriented design would be a little silly if it didn&#8217;t touch the database or the filesystem.&lt;/p&gt;


	&lt;p&gt;There is a big difference between telling the model to do something, and taking the responsibility from the model and doing the action yourself.&lt;/p&gt;


	&lt;p&gt;In regards to testing, I&#8217;ve found that it&#8217;s actually easier to test software that passes responsibility. But then again, &#8220;easier testing&#8221; and &#8220;quicker development&#8221; aren&#8217;t always synonymous.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Zach Dennis</name>
    </author>
    <id>tag:opensoul.org,2007-06-20:3089:3262</id>
    <published>2007-07-19T01:33:37Z</published>
    <updated>2007-07-19T01:33:37Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/6/20/rspec-is-getting-too-intimate-with-my-code" rel="alternate" type="text/html"/>
    <title>Comment on 'RSpec is getting too intimate with my code' by Zach Dennis</title>
<content type="html">&lt;p&gt;It&#8217;s like your saying that because a controller is a gateway into the kingdom that it should be in control of the kingdom!&lt;/p&gt;


	&lt;p&gt;For simple &lt;span class=&quot;caps&quot;&gt;CRUD&lt;/span&gt; based (or similar) actions it is easy to throw model calls into your controller and test it quickly. As models
need to interact and become more complex you shouldn&#8217;t leave the coordination of that complexity in your controller. By doing that
you violate &lt;a href=&quot;http://en.wikipedia.org/wiki/Single_responsibility_principle&quot;&gt;Single Responsibility&lt;/a&gt; and you couple business decisions with a controller (which should be a gatekeeper, not the staff manager).&lt;/p&gt;


	&lt;p&gt;The controller needs to know what &#8220;domain model&#8221; object to talk to, and then in return if things were
successful or not. The domain model object may or may not be an ActiveRecord model, that depends
on your application&#8217;s domain and the complexity of it. But that&#8217;s really about it.&lt;/p&gt;


	&lt;p&gt;Here&#8217;s oversimplified example. I have a form where the user can fill out his information and upload a
picture. In a happy day scenario you&#8217;d typically see the controller saving the user object and then if it saved, saving the picture.
The decisions that have to be made our is the user object valid, does it save, is the attachment valid, does it save?
You may want to render different information, error messages, etc based on if the user object was valid and saved or if the user object
wan&#8217;t valid, or if the user saved but the attachment wasn&#8217;t valid, or it was valid, but just didn&#8217;t save.&lt;/p&gt;


	&lt;p&gt;Why would you want that in my controller? The controller should take in the parameters and say, 
&#8220;UserModel.create_user_with_picture(params[:user], params[:picture])&#8221;. Now depending how far how you like to abstract things
perhaps the UserModel returns a errors array, or a hash of invalid objects, or perhaps it just returns a boolean.&lt;/p&gt;


	&lt;p&gt;You have put all of the business decisions of how to handle the cases in your &#8220;UserModel&#8221;, hidden away from any other object. 
If you had left this in the controller then you&#8217;d end of sprinkling around business logic between controllers and models and
again you violate Single Responsbility.&lt;/p&gt;


	&lt;p&gt;A real life example from a fellow developer revealed that a project he was consulting on had put the responsibility of controlling
model interactions in there controllers. When the customer requested that they wanted to allow some things to be done via the command
line it became a large rewrite because everything was coupled to a controller and web requests. This doesn&#8217;t happen everyday but
making better decisions daily allow these types of requests to be minimal work/effort to implement rather then having to do rewrites of
large sections of code.&lt;/p&gt;


	&lt;p&gt;It doesn&#8217;t really take any additional time upfront to keep a clean decoupled design, but it does take discipline. Creating 
reusable code is an art form.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Brandon Keepers</name>
    </author>
    <id>tag:opensoul.org,2007-06-20:3089:3196</id>
    <published>2007-07-11T17:22:44Z</published>
    <updated>2007-07-11T17:22:44Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/6/20/rspec-is-getting-too-intimate-with-my-code" rel="alternate" type="text/html"/>
    <title>Comment on 'RSpec is getting too intimate with my code' by Brandon Keepers</title>
<content type="html">&lt;p&gt;Zach &#38; Zach,&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;I actually look at it in a different light. The controller isn’t meant to control the model, but in fact to control the &lt;span class=&quot;caps&quot;&gt;HTTP&lt;/span&gt; request and response.&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;Count the number of controller actions in the latest Rails app that you&#8217;ve worked on that doesn&#8217;t touch the model. (0 for the one I&#8217;m working on right now)&lt;/p&gt;


	&lt;p&gt;I understand that controllers &#8220;control&#8221; the request and response, but they&#8217;re worthless without the ways that they &#8220;control&#8221; (or call, or manipulate, or tell the model to manipulate…whatever you want to call it) the model.  That&#8217;s why they exist.&lt;/p&gt;


	&lt;p&gt;My point is that the dependence on the model is worth the advantages of spending significantly less time testing and still having reasonable confidence that my code does what it is supposed to.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Zach Dennis</name>
    </author>
    <id>tag:opensoul.org,2007-06-20:3089:3195</id>
    <published>2007-07-11T16:37:24Z</published>
    <updated>2007-07-11T16:37:24Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/6/20/rspec-is-getting-too-intimate-with-my-code" rel="alternate" type="text/html"/>
    <title>Comment on 'RSpec is getting too intimate with my code' by Zach Dennis</title>
<content type="html">&lt;blockquote&gt;
		&lt;p&gt;I would buy that if my controllers were intended to stand alone from my object model, but they’re not! Their sole purpose is to control model objects.&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;Like Moazeni said, controller&#8217;s aren&#8217;t intended to control your ActiveRecord models. Their job is to handle incoming requests with outgoing responses.&lt;/p&gt;


	&lt;p&gt;Drew Colthorp, myself and Mark VanHolstyn have been working on a Model Conductor Pattern which is working to alleviate spreading logic around in controllers and creating a clean separation between controllers and domain logic.&lt;/p&gt;


	&lt;p&gt;In the end you end up with writing better testable code, less fragile tests and cleaner code throughout the app.&lt;/p&gt;


	&lt;p&gt;Watch the gr-ruby mailing list for when it&#8217;s going to be presented out. It should be coming up soon&#8230;. http://gr-ruby.org/2006/11/6/mailing-list&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Zach Moazeni</name>
    </author>
    <id>tag:opensoul.org,2007-06-20:3089:3123</id>
    <published>2007-06-22T18:09:33Z</published>
    <updated>2007-06-22T18:09:33Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/6/20/rspec-is-getting-too-intimate-with-my-code" rel="alternate" type="text/html"/>
    <title>Comment on 'RSpec is getting too intimate with my code' by Zach Moazeni</title>
<content type="html">&lt;blockquote&gt;
I would buy that if my controllers were intended to stand alone from my object model, but they’re not! Their sole purpose is to control model objects.
&lt;/blockquote&gt;

	&lt;p&gt;I actually look at it in a different light. The controller isn&#8217;t meant to control the model, but in fact to control the &lt;span class=&quot;caps&quot;&gt;HTTP&lt;/span&gt; request and response.&lt;/p&gt;


	&lt;p&gt;Now how much of a hand it has in actually manipulating the model is up for debate (Whether the model should know how to create itself). In the past (mainly as a Java developer) I would have said no, but working with Rails more, it seems to be common design to allow the model to have that control. The Controller does dictate the call, but doesn&#8217;t care how it&#8217;s handled. Essentially going down the route of &lt;a href=&quot;http://en.wikipedia.org/wiki/Law_Of_Demeter&quot;&gt;Law of Demeter&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;Recently I&#8217;ve been preferring that design more, especially thinking of different entry points into the application. Whether it be from a Controller, the Console, a Rake task, etc.&lt;/p&gt;


	&lt;p&gt;I do agree that moving the domain knowledge into either the Model or some other class seems to be easier to test. (You just mock the method from the controller), but it is easy to get swept away to the point where every method is a one liner to &#8220;pass the buck&#8221;.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Brandon</name>
    </author>
    <id>tag:opensoul.org,2007-06-20:3089:3115</id>
    <published>2007-06-22T06:15:29Z</published>
    <updated>2007-06-22T06:15:29Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/6/20/rspec-is-getting-too-intimate-with-my-code" rel="alternate" type="text/html"/>
    <title>Comment on 'RSpec is getting too intimate with my code' by Brandon</title>
<content type="html">&lt;blockquote&gt;
		&lt;p&gt;ThingsController isn’t (or shouldn’t be) responsible for creating a Thing. It’s responsibility is to tell Thing to create itself and respond with Thing’s result.&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;I agree, which is why it&#8217;s important that I don&#8217;t dictate in my spec how the controller tells &lt;code&gt;Thing&lt;/code&gt; to create itself.  If there was only one way to create &lt;code&gt;Thing&lt;/code&gt;, this wouldn&#8217;t be an issue.&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;The  Product.find(params[:product_id]).photos.build(params[:photo]) line is not only difficult to test ( as was pointed out already ), but makes too many assumptions—product_id must be valid, a product has an array of photos called ‘photos’, that array has a method called ‘build’. A change in any one of those facts breaks that controller unnecessarily.&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;I would buy that if my controllers were intended to stand alone from my object model, but they&#8217;re not!  Their sole purpose is to &lt;em&gt;control&lt;/em&gt; model objects.  If they are using my model objects in a way that is inconsistent with the model, I want to know about it.&lt;/p&gt;


	&lt;p&gt;The bottom line (as I mentioned in a comment above) is, while it would be a wonderful thing if I could test/spec every method apart from all the other code, there comes a point where the price I pay in time outweights the gains that I get from doing it.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>brasten</name>
    </author>
    <id>tag:opensoul.org,2007-06-20:3089:3112</id>
    <published>2007-06-22T00:35:11Z</published>
    <updated>2007-06-22T00:35:11Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/6/20/rspec-is-getting-too-intimate-with-my-code" rel="alternate" type="text/html"/>
    <title>Comment on 'RSpec is getting too intimate with my code' by brasten</title>
<content type="html">&lt;p&gt;I agree with Andrzej&#8217;s comments.&lt;/p&gt;


	&lt;p&gt;ThingsController isn&#8217;t (or shouldn&#8217;t be) responsible for creating a Thing.  It&#8217;s responsibility is to tell Thing to create itself and respond with Thing&#8217;s result.  Thing&#8217;s spec should then thoroughly test Thing&#8217;s ability to create itself.&lt;/p&gt;


	&lt;p&gt;Instead of &lt;em&gt;&#8220;should create a new thing&#8221;&lt;/em&gt;, I would say something like &lt;em&gt;&#8220;should tell thing to create a saved instance&#8221;&lt;/em&gt;, then re-write the examples accordingly.&lt;/p&gt;


	&lt;p&gt;Andrzej&#8217;s &lt;code&gt; Product.build_photo &lt;/code&gt; example would make that situation much easier to test, as well as avoid pesky &lt;a href=&quot;http://blog.jayfields.com/2006/05/law-of-demeter-and-forwardable.html&quot;&gt;law of demeter&lt;/a&gt; violations.&lt;/p&gt;


	&lt;p&gt;The &lt;code&gt; Product.find(params[:product_id]).photos.build(params[:photo])&lt;/code&gt; line is not only difficult to test ( as was pointed out already ), but makes too many assumptions&#8212;product_id must be valid, a product has an array of photos called &#8216;photos&#8217;, that array has a method called &#8216;build&#8217;.  A change in any one of those facts breaks that controller unnecessarily.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>zilkey</name>
    </author>
    <id>tag:opensoul.org,2007-06-20:3089:3107</id>
    <published>2007-06-21T08:06:51Z</published>
    <updated>2007-06-21T08:06:51Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/6/20/rspec-is-getting-too-intimate-with-my-code" rel="alternate" type="text/html"/>
    <title>Comment on 'RSpec is getting too intimate with my code' by zilkey</title>
<content type="html">&lt;p&gt;I&#8217;ve noticed lately that in my red-green-refactor process, the reds are coming more from mock and stub errors than from code errors, which may speak to my mock/stub abilities as much as the approach, but which is disheartening nonetheless.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Carl Porth</name>
    </author>
    <id>tag:opensoul.org,2007-06-20:3089:3104</id>
    <published>2007-06-20T21:20:26Z</published>
    <updated>2007-06-20T21:20:26Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/6/20/rspec-is-getting-too-intimate-with-my-code" rel="alternate" type="text/html"/>
    <title>Comment on 'RSpec is getting too intimate with my code' by Carl Porth</title>
<content type="html">&lt;p&gt;Like Ryan I also stub out the &#8220;valid?&#8221; method.  Another benefit of this trick is that it expresses the behavior more concisely then fixtures do, i.e. it &#8220;should create a new thing given valid attributes&#8221; (and all you have to do is stub &#8220;valid?&#8221; and verify a change in count).&lt;/p&gt;


	&lt;p&gt;Currently I try to avoid fixtures like the plague.  Especially with skinny controller design, the behavior at the controller level is very independent of the model layer, and therefore need not know about model specific attributes.  This makes all your controller very similar and easily maintainable.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Ryan Bates</name>
    </author>
    <id>tag:opensoul.org,2007-06-20:3089:3102</id>
    <published>2007-06-20T15:41:12Z</published>
    <updated>2007-06-20T15:41:12Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/6/20/rspec-is-getting-too-intimate-with-my-code" rel="alternate" type="text/html"/>
    <title>Comment on 'RSpec is getting too intimate with my code' by Ryan Bates</title>
<content type="html">&lt;p&gt;I have come to very similar conclusions. I &lt;a href=&quot;http://railsforum.com/viewtopic.php?id=6528&quot;&gt;posted about this&lt;/a&gt; on the railsforum.&lt;/p&gt;


	&lt;p&gt;While I hardly ever use mocks anymore, I still use stubs here and there. One case I use stubbing is to change the behavior of the validations. Instead of having a &#8220;valid_thing_attrs&#8221; method as you do, I stub out the &#8220;valid?&#8221; method to return either true/false. All create/save/update methods rely on this &#8220;valid?&#8221; method so it works really well.&lt;/p&gt;


	&lt;p&gt;As for fixtures, I went back to relying on them as well. However, the tests don&#8217;t truly rely on them, they just use them. I don&#8217;t use fixtures to test edge cases &#8211; in fact there&#8217;s only a couple records in each fixture. If I need to test an edge case then I make the models manually so the fixtures are also very flexible.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>meekish</name>
    </author>
    <id>tag:opensoul.org,2007-06-20:3089:3101</id>
    <published>2007-06-20T15:37:18Z</published>
    <updated>2007-06-20T15:37:18Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/6/20/rspec-is-getting-too-intimate-with-my-code" rel="alternate" type="text/html"/>
    <title>Comment on 'RSpec is getting too intimate with my code' by meekish</title>
<content type="html">&lt;p&gt;&#8221;...called valid_thing_attrs. I don’t remember where I picked up this pattern&#8230;&#8221;&lt;/p&gt;


	&lt;p&gt;Here&#8217;s where I picked it up: http://www.lukeredpath.co.uk/2006/8/29/developing-a-rails-model-using-bdd-and-rspec-part-1&lt;/p&gt;


	&lt;p&gt;Insightful post by the way.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Zach Moazeni</name>
    </author>
    <id>tag:opensoul.org,2007-06-20:3089:3098</id>
    <published>2007-06-20T15:25:12Z</published>
    <updated>2007-06-20T15:25:12Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/6/20/rspec-is-getting-too-intimate-with-my-code" rel="alternate" type="text/html"/>
    <title>Comment on 'RSpec is getting too intimate with my code' by Zach Moazeni</title>
<content type="html">&lt;p&gt;I feel those pains a lot too Brandon, and I agree with Andrzej. Having a lot of setup code in the tests give the &#8220;smell&#8221; that the code is trying to do too much.&lt;/p&gt;


	&lt;p&gt;Unfortunately moving the code into an abstracted version like the &lt;code&gt;Product.build_photo&lt;/code&gt; solution doesn&#8217;t always work, or you end up having tons of 1 liner methods.&lt;/p&gt;


	&lt;p&gt;My problems with fixtures isn&#8217;t the performance, but the headaches of managing lots of them. If you have a very specific corner case and you need 5 records spread across 3 models, adding additional corner cases can get spidery really quick. Or your tests become overly dependent on the relationships of the fixtures, which for me is harder to maintain than a setup method instantiating the models.&lt;/p&gt;


	&lt;p&gt;However to alleviate that problem, someone mentioned to me &lt;a href=&quot;http://code.google.com/p/fixture-scenarios/&quot;&gt;fixture_scenarios&lt;/a&gt; . Unfortunately I haven&#8217;t had a chance to play with it, but it looks very promising.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Andrzej</name>
    </author>
    <id>tag:opensoul.org,2007-06-20:3089:3097</id>
    <published>2007-06-20T13:12:26Z</published>
    <updated>2007-06-20T13:12:26Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/6/20/rspec-is-getting-too-intimate-with-my-code" rel="alternate" type="text/html"/>
    <title>Comment on 'RSpec is getting too intimate with my code' by Andrzej</title>
<content type="html">I&#8217;m not sure if I understand correctly, but maybe moving the logic to the Product class (and test it there) would solve the problem of 15-line setup?
I&#8217;d refactor this:
&lt;pre&gt;photo = Product.find(params[:product_id]).photos.build(params[:photo])
&lt;/pre&gt;
into this:
&lt;pre&gt;
Product.build_photo(params[:product_id], params[:photo])
&lt;/pre&gt;

	&lt;p&gt;Then, you just mock one method in your controller&#8217;s test.
That would also fit well with the idea of &lt;a href=&quot;http://www.robbyonrails.com/articles/2007/06/19/put-your-controllers-on-a-diet-already&quot;&gt;skinny controller, fat model&lt;/a&gt;&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>David Chelimsky</name>
    </author>
    <id>tag:opensoul.org,2007-06-20:3089:3095</id>
    <published>2007-06-20T11:46:24Z</published>
    <updated>2007-06-20T11:46:24Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/6/20/rspec-is-getting-too-intimate-with-my-code" rel="alternate" type="text/html"/>
    <title>Comment on 'RSpec is getting too intimate with my code' by David Chelimsky</title>
<content type="html">&lt;p&gt;I would change this&lt;/p&gt;


&lt;pre&gt;photo = Product.find(params[:product_id]).photos.build(params[:photo])&lt;/pre&gt;

	&lt;p&gt;to this&lt;/p&gt;


&lt;pre&gt;photo = Product.build_photo(params[:product_id),params[:photo])&lt;/pre&gt;

	&lt;p&gt;Now you have only one Message Expectation (mock) to set and you&#8217;ve happily decoupled the controller from the details of how to build photos.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Brandon</name>
    </author>
    <id>tag:opensoul.org,2007-06-20:3089:3093</id>
    <published>2007-06-20T11:10:30Z</published>
    <updated>2007-06-20T11:10:30Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/6/20/rspec-is-getting-too-intimate-with-my-code" rel="alternate" type="text/html"/>
    <title>Comment on 'RSpec is getting too intimate with my code' by Brandon</title>
<content type="html">&lt;p&gt;Andrzej,&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;Does it only apply to create/remove actions? It’s hard to test that we call the correct find_* method without depending on db fixtures.&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;Unfortunately, this approach does mean that I&#8217;m back to depending on fixtures.  But aren&#8217;t mocks just another form of fixtures in most situations?  Some good strategies for handling fixtures go a long way toward making your tests more manageable.&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;I think that one of the reasons that RSpec suggests mocking the Model part is to avoid unnecessary calls to the database.&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;A lofty goal, indeed. In practice, I&#8217;ve never had a rails test suite that was too slow.&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;Your solution is great when you often change the way the controllers interact with the Model (without changing the result), but if I don’t do it too often then mocking is also a good option (and results in faster tests).&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;In my experience, I&#8217;ve spent a lot more time writing and maintaining (and especially debugging) specs with mocks than I have with fixtures.  Any performance hit is worth the benefit of being able to develop faster and have more maintainable tests.&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;What kind of setup you need to prepare, that it takes 15 or more lines?&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;Take this line from a controller for example:&lt;/p&gt;


&lt;pre&gt;photo = Product.find(params[:product_id]).photos.build(params[:photo])
&lt;/pre&gt;

	&lt;p&gt;In that line alone, I need to stub &lt;code&gt;Product.find&lt;/code&gt;, mock the product that is returned, mock the &lt;code&gt;photos&lt;/code&gt; association, stub &lt;code&gt;photos.build&lt;/code&gt;, and mock photo that is returned.  That by itself is about 5 lines, and this is one of the most common actions in any app with nested resources.  As controllers do more (create multiple photos per controller, or update multiple objects), it only gets more complicated.&lt;/p&gt;


	&lt;p&gt;In an ideal world, every method would be tested independent of the rest of the application.  But there is a point of diminishing returns, where I just can&#8217;t justify to my clients why I&#8217;m spending so much time on testing.  Fixtures, for all their flaws, allow me to work faster, and I still have reasonable confidence that my code does what I say it does.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>David Chelimsky</name>
    </author>
    <id>tag:opensoul.org,2007-06-20:3089:3092</id>
    <published>2007-06-20T11:07:22Z</published>
    <updated>2007-06-20T11:07:22Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/6/20/rspec-is-getting-too-intimate-with-my-code" rel="alternate" type="text/html"/>
    <title>Comment on 'RSpec is getting too intimate with my code' by David Chelimsky</title>
<content type="html">&lt;p&gt;It&#8217;s all about trade-offs.&lt;/p&gt;


	&lt;p&gt;The fact that AR chooses inheritance rather than delegation puts us in a testing bind &#8211; we have to be coupled to the database OR we have to be more intimate with the implementation. We accept this design choice because we reap benefits in expressiveness and &lt;span class=&quot;caps&quot;&gt;DRY&lt;/span&gt;-ness.&lt;/p&gt;


	&lt;p&gt;In grappling with the dilemma, I chose faster tests at the cost of slightly more brittle. You&#8217;re choosing less brittle tests at the cost of them running slightly slower. It&#8217;s a trade-off either way.&lt;/p&gt;


	&lt;p&gt;In practice, I run the tests hundreds, if not thousands, of times a day (I use autotest and take very granular steps) and I change whether I use &#8220;new&#8221; or &#8220;create&#8221; almost never. Also due to granular steps, new models that appear are quite volatile at first. The valid_thing_attrs approach minimizes the pain from this a bit, but it still means that every new required field means that I have to change valid_thing_attrs.&lt;/p&gt;


	&lt;p&gt;But if your approach is working for you in practice, then its good! In fact, I&#8217;d strongly recommend that you publish a plugin with generators that produce the examples the way you like them. I&#8217;m sure that a lot of people would benefit from that.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Andrzej</name>
    </author>
    <id>tag:opensoul.org,2007-06-20:3089:3090</id>
    <published>2007-06-20T10:16:49Z</published>
    <updated>2007-06-20T10:16:49Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/6/20/rspec-is-getting-too-intimate-with-my-code" rel="alternate" type="text/html"/>
    <title>Comment on 'RSpec is getting too intimate with my code' by Andrzej</title>
<content type="html">&lt;p&gt;Thanks for sharing your idea. I find it very interesting!&lt;/p&gt;


	&lt;p&gt;Does it only apply to create/remove actions? It&#8217;s hard to test that we call the correct find_* method without depending on db fixtures.&lt;/p&gt;


	&lt;p&gt;I think that one of the reasons that RSpec suggests mocking the Model part is to avoid unnecessary calls to the database. Your solution is great when you often change the way the controllers interact with the Model (without changing the result), but if I don&#8217;t do it too often then mocking is also a good option (and results in faster tests).&lt;/p&gt;


	&lt;p&gt;What kind of setup you need to prepare, that it takes 15 or more lines?&lt;/p&gt;


	&lt;p&gt;I like the valid_thing_attrs trick :)&lt;/p&gt;</content>  </entry>
</feed>
