<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: My top 7 RSpec best practices</title>
	<atom:link href="http://kpumuk.info/ruby-on-rails/my-top-7-rspec-best-practices/feed/" rel="self" type="application/rss+xml" />
	<link>http://kpumuk.info/ruby-on-rails/my-top-7-rspec-best-practices/</link>
	<description>In my blog I'll try to describe about interesting technologies, my discovery in IT and some useful things about programming.</description>
	<lastBuildDate>Tue, 29 Dec 2009 10:34:28 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Dmytro Shteflyuk</title>
		<link>http://kpumuk.info/ruby-on-rails/my-top-7-rspec-best-practices/#comment-285492</link>
		<dc:creator>Dmytro Shteflyuk</dc:creator>
		<pubDate>Wed, 17 Mar 2010 00:23:00 +0000</pubDate>
		<guid isPermaLink="false">http://kpumuk.info/?p=1080#comment-285492</guid>
		<description>Hey Myron, thank you for the reply. I have updated an article with more consistent [cci_ruby]context[/cci_ruby] usage, it was my failure.

About mocking in controllers, yes it makes sense. Sometimes. When you pretty sure you models work as expected, when you confident in their work. As I said, in some circumstances you can get false positives, for example, when add [cci_ruby]after_create[/cci_ruby] hook which fails with an exception, or new super-useful [cci_sql]NOT NULL[/cci_sql] restriction on one of columns in the database. If you have all your models covered with — it&#039;s ok, use mocks to get controller specs clean in simple. By the way, did you hear about &lt;a href=&quot;http://rspec.rubyforge.org/rspec-rails/1.2.9/classes/Spec/Rails/Mocks.html#M000014&quot; rel=&quot;nofollow&quot;&gt;stub_model&lt;/a&gt;?</description>
		<content:encoded><![CDATA[<p>Hey Myron, thank you for the reply. I have updated an article with more consistent <code class="codecolorer ruby default"><span class="ruby">context</span></code> usage, it was my failure.</p>
<p>About mocking in controllers, yes it makes sense. Sometimes. When you pretty sure you models work as expected, when you confident in their work. As I said, in some circumstances you can get false positives, for example, when add <code class="codecolorer ruby default"><span class="ruby">after_create</span></code> hook which fails with an exception, or new super-useful <code class="codecolorer sql default"><span class="sql"><span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span></span></code> restriction on one of columns in the database. If you have all your models covered with — it&#8217;s ok, use mocks to get controller specs clean in simple. By the way, did you hear about <a href="http://rspec.rubyforge.org/rspec-rails/1.2.9/classes/Spec/Rails/Mocks.html#M000014" rel="nofollow">stub_model</a>?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Myron</title>
		<link>http://kpumuk.info/ruby-on-rails/my-top-7-rspec-best-practices/#comment-285487</link>
		<dc:creator>Myron</dc:creator>
		<pubDate>Wed, 17 Mar 2010 19:44:00 +0000</pubDate>
		<guid isPermaLink="false">http://kpumuk.info/?p=1080#comment-285487</guid>
		<description>These are some good tips, but I don&#039;t fully agree with all them.  Particular points 4 and 5:

About mocks: In general, I agree with you that over mocking is a problem.  But I disagree that it&#039;s always better to use no mocking or stubbing in your controller specs.  In my opinion, the danger of mocking is directly proportional to how likely it is the interface you are mocking will change.  So, mocking a class method that frequently changes its interface is dangerous and should be avoided.  Mocking an interface that is highly unlikely to change is generally safe and can lead to less brittle tests.

So, for example, when I&#039;m spec&#039;ing the #update and #create actions of a controller, I mock #valid? so that it returns true or false based on which code path I want to test.  The alternative is to pass valid or invalid attributes in the request, but this leads to fragile specs: now my controller specs have to know what attribute values are valid and invalid, and when my model validation logic changes, my controller specs begin to fail.  Really, I don&#039;t care in my controller specs what is valid and invalid...I just care that when the attributes are valid, it does one thing, and when they are invalid, it does another thing.  #valid? has been around in ActiveRecord for as long as I can remember (I imagine it was there from the first release) and the likely hood of that interface ever changing is approximately nil.  I think it&#039;s safe and useful to use mocking here.

About describe vs. context: In your example, you use context for both &quot;.top&quot; and &quot;when just created&quot;.  I think context is appropriate for the latter (since &quot;when just created&quot; describes a context the examples run in) but describe is more appropriate for the former.  &quot;.top&quot; isn&#039;t a context; rather it&#039;s simply grouping all the examples where you are describing the top method.  I use use describe in these cases since you&#039;re describing the behavior of a method.</description>
		<content:encoded><![CDATA[<p>These are some good tips, but I don&#8217;t fully agree with all them.  Particular points 4 and 5:</p>
<p>About mocks: In general, I agree with you that over mocking is a problem.  But I disagree that it&#8217;s always better to use no mocking or stubbing in your controller specs.  In my opinion, the danger of mocking is directly proportional to how likely it is the interface you are mocking will change.  So, mocking a class method that frequently changes its interface is dangerous and should be avoided.  Mocking an interface that is highly unlikely to change is generally safe and can lead to less brittle tests.</p>
<p>So, for example, when I&#8217;m spec&#8217;ing the #update and #create actions of a controller, I mock #valid? so that it returns true or false based on which code path I want to test.  The alternative is to pass valid or invalid attributes in the request, but this leads to fragile specs: now my controller specs have to know what attribute values are valid and invalid, and when my model validation logic changes, my controller specs begin to fail.  Really, I don&#8217;t care in my controller specs what is valid and invalid&#8230;I just care that when the attributes are valid, it does one thing, and when they are invalid, it does another thing.  #valid? has been around in ActiveRecord for as long as I can remember (I imagine it was there from the first release) and the likely hood of that interface ever changing is approximately nil.  I think it&#8217;s safe and useful to use mocking here.</p>
<p>About describe vs. context: In your example, you use context for both &#8220;.top&#8221; and &#8220;when just created&#8221;.  I think context is appropriate for the latter (since &#8220;when just created&#8221; describes a context the examples run in) but describe is more appropriate for the former.  &#8220;.top&#8221; isn&#8217;t a context; rather it&#8217;s simply grouping all the examples where you are describing the top method.  I use use describe in these cases since you&#8217;re describing the behavior of a method.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: erka</title>
		<link>http://kpumuk.info/ruby-on-rails/my-top-7-rspec-best-practices/#comment-285452</link>
		<dc:creator>erka</dc:creator>
		<pubDate>Wed, 17 Mar 2010 14:11:00 +0000</pubDate>
		<guid isPermaLink="false">http://kpumuk.info/?p=1080#comment-285452</guid>
		<description>Hey FlyboyArt,

Okay. Please, share your experience with us. What makes you sure that your code working and working right? How easy will it be for you to change some logic in your project or just do a refactoring and be sure that you didn&#039;t broke something your project?</description>
		<content:encoded><![CDATA[<p>Hey FlyboyArt,</p>
<p>Okay. Please, share your experience with us. What makes you sure that your code working and working right? How easy will it be for you to change some logic in your project or just do a refactoring and be sure that you didn&#8217;t broke something your project?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: FlyboyArt</title>
		<link>http://kpumuk.info/ruby-on-rails/my-top-7-rspec-best-practices/#comment-285434</link>
		<dc:creator>FlyboyArt</dc:creator>
		<pubDate>Wed, 17 Mar 2010 16:52:00 +0000</pubDate>
		<guid isPermaLink="false">http://kpumuk.info/?p=1080#comment-285434</guid>
		<description>I&#039;ve tried using Rspec before and find that it takes longer to code the tests than to write the code correct in the first place. Maybe I&#039;ve been doing this far too long (35 years now). Perhaps this system is useful for teams of people working on a project but for a single developer, I really don&#039;t see the advantage of spending the time doing this when the time could be spent more productively.</description>
		<content:encoded><![CDATA[<p>I&#8217;ve tried using Rspec before and find that it takes longer to code the tests than to write the code correct in the first place. Maybe I&#8217;ve been doing this far too long (35 years now). Perhaps this system is useful for teams of people working on a project but for a single developer, I really don&#8217;t see the advantage of spending the time doing this when the time could be spent more productively.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dmytro Shteflyuk</title>
		<link>http://kpumuk.info/ruby-on-rails/my-top-7-rspec-best-practices/#comment-285240</link>
		<dc:creator>Dmytro Shteflyuk</dc:creator>
		<pubDate>Wed, 17 Mar 2010 18:55:00 +0000</pubDate>
		<guid isPermaLink="false">http://kpumuk.info/?p=1080#comment-285240</guid>
		<description>The only difference is that [cci]rake spec[/cci] executes [cci]db:test:prepare[/cci] before running your test suite. It means, that [cci]spec spec[/cci] is faster (on large databases — significantly faster), but you will have to run [cci]rake db:migrate RAILS_ENV=test[/cci] manually to ensure your test database is up to day.</description>
		<content:encoded><![CDATA[<p>The only difference is that <code class="codecolorer text default"><span class="text">rake spec</span></code> executes <code class="codecolorer text default"><span class="text">db:test:prepare</span></code> before running your test suite. It means, that <code class="codecolorer text default"><span class="text">spec spec</span></code> is faster (on large databases — significantly faster), but you will have to run <code class="codecolorer text default"><span class="text">rake db:migrate RAILS_ENV=test</span></code> manually to ensure your test database is up to day.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: songrit</title>
		<link>http://kpumuk.info/ruby-on-rails/my-top-7-rspec-best-practices/#comment-285239</link>
		<dc:creator>songrit</dc:creator>
		<pubDate>Wed, 17 Mar 2010 18:20:00 +0000</pubDate>
		<guid isPermaLink="false">http://kpumuk.info/?p=1080#comment-285239</guid>
		<description>[cci]rake spec[/cci] and [cci]spec spec[/cci] return different results on my Rails app. Why are they different and how to determine when to use which command. From my observation, one use data from development db and the other use data from fixture.</description>
		<content:encoded><![CDATA[<p><code class="codecolorer text default"><span class="text">rake spec</span></code> and <code class="codecolorer text default"><span class="text">spec spec</span></code> return different results on my Rails app. Why are they different and how to determine when to use which command. From my observation, one use data from development db and the other use data from fixture.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dmytro Shteflyuk</title>
		<link>http://kpumuk.info/ruby-on-rails/my-top-7-rspec-best-practices/#comment-285205</link>
		<dc:creator>Dmytro Shteflyuk</dc:creator>
		<pubDate>Wed, 17 Mar 2010 22:23:00 +0000</pubDate>
		<guid isPermaLink="false">http://kpumuk.info/?p=1080#comment-285205</guid>
		<description>BTW, spec command started support [cci]spec/spec.opts[/cci] starting from 1.2.9, the latest version for today.</description>
		<content:encoded><![CDATA[<p>BTW, spec command started support <code class="codecolorer text default"><span class="text">spec/spec.opts</span></code> starting from 1.2.9, the latest version for today.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shot</title>
		<link>http://kpumuk.info/ruby-on-rails/my-top-7-rspec-best-practices/#comment-285204</link>
		<dc:creator>Shot</dc:creator>
		<pubDate>Wed, 17 Mar 2010 21:59:00 +0000</pubDate>
		<guid isPermaLink="false">http://kpumuk.info/?p=1080#comment-285204</guid>
		<description>Well, there must be a way to configure the text editors and IDEs (I just checked rspec-tmbundle’s [cci_ruby]Spec::Mate::Runner[/cci_ruby] and it takes arbitrary options everywhere it makes sense to do so, and given that it’s RSpec’s David Chelimsky’s code, I’d be surprised if it didn’t handle the spec/spec.opts case sanely or even defaulted to it).

I don’t really get the ‘&lt;em&gt;there is too much work to exclude this single line from specs&lt;/em&gt;’ comment; I simply never put any such line in my specs to begin with (I hate clutter and never understood the popular custom of starting each and every spec with the same noise line, when the same behaviour can be configured in one place).

But I definitely don’t want to evangelise; I just wanted to share my (IMHO, saner and more elegant) solution. Thanks for all the other great tips! :)</description>
		<content:encoded><![CDATA[<p>Well, there must be a way to configure the text editors and IDEs (I just checked rspec-tmbundle’s <code class="codecolorer ruby default"><span class="ruby"><span style="color:#6666ff; font-weight:bold;">Spec::Mate::Runner</span></span></code> and it takes arbitrary options everywhere it makes sense to do so, and given that it’s RSpec’s David Chelimsky’s code, I’d be surprised if it didn’t handle the spec/spec.opts case sanely or even defaulted to it).</p>
<p>I don’t really get the ‘<em>there is too much work to exclude this single line from specs</em>’ comment; I simply never put any such line in my specs to begin with (I hate clutter and never understood the popular custom of starting each and every spec with the same noise line, when the same behaviour can be configured in one place).</p>
<p>But I definitely don’t want to evangelise; I just wanted to share my (IMHO, saner and more elegant) solution. Thanks for all the other great tips! :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dmytro Shteflyuk</title>
		<link>http://kpumuk.info/ruby-on-rails/my-top-7-rspec-best-practices/#comment-285200</link>
		<dc:creator>Dmytro Shteflyuk</dc:creator>
		<pubDate>Wed, 17 Mar 2010 14:46:00 +0000</pubDate>
		<guid isPermaLink="false">http://kpumuk.info/?p=1080#comment-285200</guid>
		<description>Okay, you&#039;re right here. But what about Textmate and other editors (or even IDEs like RubyMine) which support &quot;Run Examples&quot; or &quot;Run current Example&quot;? :-)

There is too much work to exclude this single line from specs.</description>
		<content:encoded><![CDATA[<p>Okay, you&#8217;re right here. But what about Textmate and other editors (or even IDEs like RubyMine) which support &#8220;Run Examples&#8221; or &#8220;Run current Example&#8221;? :-)</p>
<p>There is too much work to exclude this single line from specs.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shot</title>
		<link>http://kpumuk.info/ruby-on-rails/my-top-7-rspec-best-practices/#comment-285198</link>
		<dc:creator>Shot</dc:creator>
		<pubDate>Wed, 17 Mar 2010 14:35:00 +0000</pubDate>
		<guid isPermaLink="false">http://kpumuk.info/?p=1080#comment-285198</guid>
		<description>If you rather use the [cci]spec[/cci] command, you’d be thrilled to know that it actually defaults to fetch the options from [cci]spec/spec.opts[/cci] (and, of course, you could always explicitely [cci_bash]alias spec=&#039;spec -O spec/spec.opts&#039;[/cci_bash]). :)</description>
		<content:encoded><![CDATA[<p>If you rather use the <code class="codecolorer text default"><span class="text">spec</span></code> command, you’d be thrilled to know that it actually defaults to fetch the options from <code class="codecolorer text default"><span class="text">spec/spec.opts</span></code> (and, of course, you could always explicitely <code class="codecolorer bash default"><span class="bash"><span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">spec</span>=<span style="color: #ff0000;">'spec -O spec/spec.opts'</span></span></code>). :)</p>
]]></content:encoded>
	</item>
</channel>
</rss>
