<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Object Mentor Blog: Specs vs. Tests</title>
    <link>http://blog.objectmentor.com/articles/2007/02/01/specs-vs-tests</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>Specs vs. Tests</title>
      <description>&lt;p&gt;There&amp;#8217;s something to this &lt;span class="caps"&gt;BDD&lt;/span&gt; kool-aid that people have been drinking lately&amp;#8230;&lt;/p&gt;


	&lt;p&gt;As part of the Rails project I&amp;#8217;ve been working on for the last few weeks, I&amp;#8217;ve been using &lt;a href="http://rspec.rubyforge.org/"&gt;RSpec&lt;/a&gt;.  RSpec is a unit testing tool similar in spirit to &lt;a href="www.junit.org"&gt;JUnit&lt;/a&gt; or &lt;a href="http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/index.html"&gt;Test/Unit&lt;/a&gt;.  However RSpec uses an alternative syntax that reads more like a &lt;em&gt;specification&lt;/em&gt; than like a test. Let me show you what I mean.&lt;/p&gt;


In Java, using JUnit, we might write the following unit test:
&lt;pre&gt;&lt;code&gt;public class BowlingGameTest extends TestCase {
  private Game g;

  protected void setUp() throws Exception {
    g = new Game();
  }

  private void rollMany(int n, int pins) {
    for (int i=0; i&amp;lt; n; i++) {
      g.roll(pins);
    }
  }

  public void testGutterGame() throws Exception {
    rollMany(20, 0);
    assertEquals(0, g.score());
    assertTrue(g.isComplete());
  }

  public void testAllOnes() throws Exception {
    rollMany(20,1);
    assertEquals(20, g.score());
    assertTrue(g.isComplete());
  }
}&lt;/code&gt;&lt;/pre&gt;
This is pretty typical for a Java unit test.  The setup function builds the &lt;code&gt;Game&lt;/code&gt; object, and then the various test functions make sure that it works in each different scenario.

In Ruby however, this might be expressed using RSpec as:
&lt;pre&gt;&lt;code&gt;require 'rubygems'
require_gem "rspec" 
require 'game'

context "When a gutter game is rolled" do
  setup do
    @g = Game.new
    20.times {@g.roll 0}
  end

  specify "score should be zero" do
    @g.score.should == 0
  end

  specify "game should be complete" do
    @g.complete?.should_be true
  end
end

context "When all ones are rolled" do
  setup do
    @g = Game.new
    20.times{@g.roll 1}
  end

  specify "score should be 20" do
    @g.score.should == 20
  end

  specify "game should be complete" do
    @g.complete?.should_be true
  end
end
&lt;/code&gt;&lt;/pre&gt;
At first blush the difference seems small.  Indeed, the RSpec code might seem too verbose and fine-grained.  At least that was my first impression when I first saw RSpec.  However, having used it now for several months I have a different reaction.

	&lt;p&gt;First, let&amp;#8217;s looks a the semantic differences.  In JUnit you have &lt;code&gt;TestCase&lt;/code&gt; derivatives, and test functions.  Each &lt;code&gt;TestCase&lt;/code&gt; derivative has a &lt;code&gt;setUp&lt;/code&gt; and &lt;code&gt;tearDown&lt;/code&gt; function, and a suite of &lt;code&gt;test&lt;/code&gt; functions.  In RSpec you have what appears to be an extra layer.  You have the test script, which is composed of &lt;code&gt;context&lt;/code&gt; blocks.  The contexts have &lt;code&gt;setup&lt;/code&gt;, &lt;code&gt;teardown&lt;/code&gt;, and &lt;code&gt;specify&lt;/code&gt; blocks.&lt;/p&gt;


	&lt;p&gt;At first you might think that the RSpec &lt;code&gt;context&lt;/code&gt; block coresponds to the Java &lt;code&gt;TestCase&lt;/code&gt; derivative since they are semantically equivalent.  However Java throws something of a curve at us by only allowing one public class per file.  So from an organizational point of view there is a stronger equivalence between the &lt;code&gt;TestCase&lt;/code&gt; derivative and the whole RSpec test script.&lt;/p&gt;


	&lt;p&gt;This might seem petty.  After all, I can write Java code that is semantically equivalent to the RSpec code simply by creating two &lt;code&gt;TestCase&lt;/code&gt; derivatives in two different files.  But separating those two test cases into two different files makes a big difference to me.  It breaks apart things that otherwise want to stay together.&lt;/p&gt;


	&lt;p&gt;Now it&amp;#8217;s true that I could keep the &lt;code&gt;TestCase&lt;/code&gt; derivatives in the same file  by making them &lt;code&gt;package&lt;/code&gt; scope, and manually put them into a public &lt;code&gt;TestSuite&lt;/code&gt; class.  But who wants to do that?  After all, my &lt;span class="caps"&gt;IDE&lt;/span&gt; is nice enough to find and execute all the &lt;em&gt;public&lt;/em&gt; &lt;code&gt;TestCase&lt;/code&gt; derivatives, which completely eliminates the need for me to build suites&amp;#8212;at least at first.&lt;/p&gt;


	&lt;table&gt;
		&lt;tr style="background:#d0d0ff;color:blue;"&gt;
			&lt;td&gt;Note: The &lt;a href="http://www.laughingpanda.org/projects/jdave/"&gt;JDave&lt;/a&gt; tool provides &lt;span class="caps"&gt;BDD&lt;/span&gt; syntax for Java.&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/table&gt;




	&lt;p&gt;Again, this might seem petty; and if that were the only benefit to the RSpec syntax I would agree.  But it&amp;#8217;s not the only benefit.&lt;/p&gt;


	&lt;p&gt;Strange though it may seem, the next benefit is the &lt;em&gt;strings&lt;/em&gt; that describe the &lt;code&gt;context&lt;/code&gt; and &lt;code&gt;specify&lt;/code&gt; blocks.  At first I thought these strings were just noise, like the strings in the JUnit &lt;code&gt;assert&lt;/code&gt; functions.  I seldom, if ever, use the JUnit &lt;code&gt;assert&lt;/code&gt; strings, so why would I use the &lt;code&gt;context&lt;/code&gt; and &lt;code&gt;specify&lt;/code&gt; strings?  But over the last few weeks I have come to find that, unlike the JUnit &lt;code&gt;assert&lt;/code&gt; strings, the RSpec strings put a subtle force on me to create better test designs.&lt;/p&gt;


	&lt;h3&gt;&lt;em&gt;Stable State&lt;/em&gt;: An Emergent Rule.&lt;/h3&gt;


	&lt;p&gt;When a spec fails, the message that gets printed is the concatenation of the &lt;code&gt;context&lt;/code&gt; string and the &lt;code&gt;specify&lt;/code&gt; string.  For example: &lt;code&gt;'When a gutter game is rolled game should be complete' FAILED&lt;/code&gt;.  If you word the context and specify strings properly, these error message make nice sentences.  Since, in &lt;span class="caps"&gt;TDD&lt;/span&gt;, we almost always start out with our tests failing, I see these error message a lot.  So there is a pressure on me to word them well.&lt;/p&gt;


	&lt;p&gt;But by wording them well, I am constrained to obey a rule that JUnit never put pressure on me to obey.  Indeed, I didn&amp;#8217;t know it &lt;em&gt;was&lt;/em&gt; a rule until I started using RSpec.  I call this rule &lt;em&gt;Stable State&lt;/em&gt;, it is:&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;Tests don&amp;#8217;t change the state.&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;In other words, the functions that make assertions about the state of the system, do not also change the state of the system.  The state of the system is set up once in the &lt;code&gt;setUp&lt;/code&gt; function, and then only interrogated by the &lt;code&gt;test&lt;/code&gt; functions.&lt;/p&gt;


	&lt;p&gt;If you look carefully at the specification of the Bowling Game you will see that the state of the &lt;code&gt;Game&lt;/code&gt; is changed only by the &lt;code&gt;setup&lt;/code&gt; block within the &lt;code&gt;context&lt;/code&gt; blocks.  The &lt;code&gt;specify&lt;/code&gt; blocks simply interrogate and verify state.  This is in stark contrast to the JUnit tests in which the &lt;code&gt;test&lt;/code&gt; methods both change and verify the state of the &lt;code&gt;Game&lt;/code&gt;.&lt;/p&gt;


	&lt;p&gt;If you don&amp;#8217;t follow this rule it is hard to get the strings on the &lt;code&gt;context&lt;/code&gt; and &lt;code&gt;specify&lt;/code&gt; blocks to create error messages that read well.  On the other hand, if you make sure that the &lt;code&gt;specify&lt;/code&gt; blocks don&amp;#8217;t change the state, then you can find simple sentences that describe each &lt;code&gt;context&lt;/code&gt; and &lt;code&gt;specify&lt;/code&gt; block.  And so the subtle pressure of the strings has a significant impact on the structure of the tests.&lt;/p&gt;


	&lt;p&gt;I can&amp;#8217;t claim to have discovered the pressure of these strings.  Indeed, Dan North&amp;#8217;s &lt;a href="http://dannorth.net/introducing-bdd/"&gt;original article&lt;/a&gt; on the topic is captivating.  However, I felt the pressure and came to the same conclusion he did, well before I read his article; simply by using a tool inspired by his work.&lt;/p&gt;


	&lt;p&gt;The benefit of &lt;em&gt;Stable State&lt;/em&gt; is that for each set of assertions there is one, and only one place where the state of the system is changed.  Moreover the three level structure provides natural places for groups of state, states, and asserts.&lt;/p&gt;


	&lt;h3&gt;The demise of the &lt;em&gt;One Assert&lt;/em&gt; rule.&lt;/h3&gt;


	&lt;p&gt;There have been other rules like this before.  One that circulated a few years back was:&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;One assert per test.&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;I never bought into this rule, and I still don&amp;#8217;t.  It seems arbitrary and inefficient.  Why should I put each &lt;code&gt;assert&lt;/code&gt; statement into it&amp;#8217;s own test method when I can just as well put the &lt;code&gt;assert&lt;/code&gt; statement into a single test method.&lt;/p&gt;


In other words, why prefer this:
&lt;pre&gt;&lt;code&gt;  public void testGutterGameScoreIsZero() throws Exception {
    rollMany(20, 0);
    assertEquals(0, g.score());
  }

  public void testGutterGameIsComplete() throws Exception {
    rollMany(20, 0);
    assertTrue(g.isComplete());
  }&lt;/code&gt;&lt;/pre&gt;
over this:
&lt;pre&gt;&lt;code&gt;  public void testGutterGame() throws Exception {
    rollMany(20, 0);
    assertEquals(0, g.score());
    assertTrue(g.isComplete());
  }&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;I think the authors of the &lt;em&gt;One Assert&lt;/em&gt; rule were trying to achieve the benefits of &lt;em&gt;Stable State&lt;/em&gt;, but missed the mark.  It&amp;#8217;s as though they could smell the rule out there, but couldn&amp;#8217;t quite pinpoint it.&lt;/p&gt;


	&lt;h3&gt;The &lt;em&gt;State Machine&lt;/em&gt; metaphor&lt;/h3&gt;


	&lt;p&gt;When you follow the &lt;em&gt;Stable State&lt;/em&gt; rule your specifications (tests) become a description of a Finite State Machine.  Each &lt;code&gt;context&lt;/code&gt; block describes how to drive the &lt;span class="caps"&gt;SUT&lt;/span&gt; to a given state, and then the &lt;code&gt;specify&lt;/code&gt; blocks describe the attributes of that state.&lt;/p&gt;


	&lt;p&gt;Dan North calls this the &lt;em&gt;Given-When-Then&lt;/em&gt; metaphor.  Consider the following triplet:&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;&lt;strong&gt;Given&lt;/strong&gt; a Bowling Game:
&lt;strong&gt;When&lt;/strong&gt; 20 gutter balls are rolled,
&lt;strong&gt;Then&lt;/strong&gt; the score should be zero and the game should be complete.&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;This triplet corresponds nicely to a row in a &lt;em&gt;state transition table&lt;/em&gt;.  Consider, for example, the subway turnstile state machine:&lt;/p&gt;


	&lt;table style="border:1px solid black;padding:2px;"&gt;
		&lt;tr&gt;
			&lt;th&gt;Current State&lt;/th&gt;
			&lt;th&gt;Event&lt;/th&gt;
			&lt;th&gt;New State&lt;/th&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;Locked&lt;/td&gt;
			&lt;td&gt;coin&lt;/td&gt;
			&lt;td&gt;Unlocked&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;Unlocked&lt;/td&gt;
			&lt;td&gt;pass&lt;/td&gt;
			&lt;td&gt;Locked&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;Locked&lt;/td&gt;
			&lt;td&gt;pass&lt;/td&gt;
			&lt;td&gt;Alarm&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;Unlocked&lt;/td&gt;
			&lt;td&gt;coin&lt;/td&gt;
			&lt;td&gt;Unlocked&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/table&gt;




	&lt;p&gt;We can read this as follows:&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;&lt;span class="caps"&gt;GIVEN&lt;/span&gt; we are in the Locked state, 
&lt;span class="caps"&gt;WHEN&lt;/span&gt; we get a coin event, 
&lt;span class="caps"&gt;THEN&lt;/span&gt; we should be in the Unlocked state.
&lt;del&gt;&amp;#8212;&lt;/del&gt;
&lt;span class="caps"&gt;GIVEN&lt;/span&gt; we are in the Unlocked state, 
&lt;span class="caps"&gt;WHEN&lt;/span&gt; we get a pass event, 
&lt;span class="caps"&gt;THEN&lt;/span&gt; we should be in the Locked state.
&lt;del&gt;&amp;#8212;&lt;/del&gt;
etc.&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;Describing a system as a finite state machine has certain benefits.&lt;/p&gt;


	&lt;ol&gt;
	&lt;li&gt;We can enumerate the states and the events, and then make sure that every combination of state and event is handled properly.&lt;/li&gt;
		&lt;li&gt;We can formalize the behavior of the system into a well known tabular format that can be read and interpreted by machines.
	&lt;ul&gt;
	&lt;li&gt;I am, of course, thinking about &lt;a href="www.fitnesse.org"&gt;FitNesse&lt;/a&gt;&lt;/li&gt;
	&lt;/ul&gt;
	&lt;/li&gt;
		&lt;li&gt;There are well known mechanisms for implementing finite state machines.&lt;/li&gt;
	&lt;/ol&gt;


	&lt;p&gt;The point is that organizing the system description in terms of a finite state machine can have a profound impact on the system design and implementation.&lt;/p&gt;


	&lt;h3&gt;The Butterfly Effect.&lt;/h3&gt;


	&lt;p&gt;I find it remarkable that two dumb annoying little strings put a subtle pressure on me to adjust the style of my tests. That change in style eventually caused me to see the design and implementation of the system I was writing in a very new and interesting light.&lt;/p&gt;</description>
      <pubDate>Thu, 01 Feb 2007 09:32:07 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:8dcd6d9a-ed8c-4cd2-9522-24dc5e67ac19</guid>
      <author>Uncle Bob</author>
      <link>http://blog.objectmentor.com/articles/2007/02/01/specs-vs-tests</link>
      <category>Uncle Bob's Blatherings</category>
      <trackback:ping>http://blog.objectmentor.com/articles/trackback/110</trackback:ping>
    </item>
    <item>
      <title>"Specs vs. Tests" by Jason Gorman</title>
      <description>&lt;p&gt;Courtesy of the WaybackWhen web cache (interview from 2003, I think):&lt;/p&gt;


	&lt;p&gt;ObjectMonkey: Here&amp;#8217;s a hot potato for you &amp;#8211; is Test-driven Development really Formal Methods in disguise?&lt;/p&gt;


	&lt;p&gt;Uncle Bob: Test-driven development is the most profound and auspicious thing to happen to the software industry since I&amp;#8217;ve been a programmer. I think it&amp;#8217;s even more important than OO.&lt;/p&gt;


	&lt;p&gt;ObjectMonkey: I&amp;#8217;m inclined to agree.&lt;/p&gt;


	&lt;p&gt;Uncle Bob: Nothing has had such a profound effect upon the way I write code than TDD. Nothing. When I write code now, I run tests every few minutes. My stuff is always working. I never have windows all over my screen with modules torn apart, hoping I can one day piece them back together again. Every minute or two I run tests, and get my stuff working. I don&amp;#8217;t use debuggers anymore. Debuggers are a drug. You get addicted to them. They drag you down a rat hole. You spin and spin, trying to set your breakpoints, trying to follow the logic, trying to figure out what the hell is going on. With TDD, that all but goes away. I haven&amp;#8217;t used a debugger in anger in over three years. And I chide anyone I see who is using one. &lt;strong&gt;So I don&amp;#8217;t care whether there is a link between TDD and FM&lt;/strong&gt;. TDD is a great boon to me, and to software in general.&#239;&#191;&#189;&lt;/p&gt;</description>
      <pubDate>Wed, 14 Feb 2007 10:48:19 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:d17879a2-00ba-4b2f-8e5f-864f3505a2b0</guid>
      <link>http://blog.objectmentor.com/articles/2007/02/01/specs-vs-tests#comment-109</link>
    </item>
    <item>
      <title>"Specs vs. Tests" by Jason Gorman</title>
      <description>&lt;p&gt;That&amp;#8217;s not to say I don&amp;#8217; totally agree with what you&amp;#8217;re saying now. I think any movement towards higher integrity specs &amp;#8211; executable specs &amp;#8211; is progress. It all sounds very familiar to me &amp;#8211; I think I&amp;#8217;ve been doing BDD right from the get go since I started doing TDD &amp;#8211; so I&amp;#8217;m bound to draw the comparison now.&lt;/p&gt;</description>
      <pubDate>Wed, 14 Feb 2007 09:57:29 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:79a4b5b9-9a31-471c-9f78-3787401fcae4</guid>
      <link>http://blog.objectmentor.com/articles/2007/02/01/specs-vs-tests#comment-108</link>
    </item>
    <item>
      <title>"Specs vs. Tests" by Jason Gorman</title>
      <description>&lt;p&gt;Alas, I don&amp;#8217;t have the original manuscript to hand, but from memory I think I asked you if TDD was formal specification by the back door, and I distinctly recall you saying you didn&amp;#8217;t care if it was. The title of the interview was &amp;#8220;Getting Sh*t Done&amp;#8221;, if that helps establish the context :-)&lt;/p&gt;</description>
      <pubDate>Wed, 14 Feb 2007 09:46:56 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:a1d9f81a-d2b4-4db6-bda5-b8282329b548</guid>
      <link>http://blog.objectmentor.com/articles/2007/02/01/specs-vs-tests#comment-107</link>
    </item>
    <item>
      <title>"Specs vs. Tests" by Uncle Bob</title>
      <description>&lt;p&gt;Jason,  You&amp;#8217;ll have to refresh my memory about that interview and the context of it.  I&amp;#8217;ve been making the &amp;#8220;formal document&amp;#8221; argument for at least five years.&lt;/p&gt;</description>
      <pubDate>Fri, 09 Feb 2007 06:46:26 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:177e6795-dde5-4a80-bb96-b0e70d7619c5</guid>
      <link>http://blog.objectmentor.com/articles/2007/02/01/specs-vs-tests#comment-99</link>
    </item>
    <item>
      <title>"Specs vs. Tests" by Uncle Bob</title>
      <description>&lt;p&gt;rspec in jRuby&amp;#8230;  now &lt;em&gt;that&amp;#8217;s&lt;/em&gt; an interesting thought&amp;#8230;&lt;/p&gt;</description>
      <pubDate>Fri, 09 Feb 2007 06:45:36 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:db8e9836-44b6-4872-bfd7-124b0ff0bd64</guid>
      <link>http://blog.objectmentor.com/articles/2007/02/01/specs-vs-tests#comment-98</link>
    </item>
    <item>
      <title>"Specs vs. Tests" by Paul Davis</title>
      <description>&lt;p&gt;After reading this article, I&amp;#8217;m wondering&amp;#8230;.
Could we use jRuby to write RSpec tests against java code?
Damn Uncle Bob, I&amp;#8217;m going to lose another weekend because of you.
;-)&lt;/p&gt;</description>
      <pubDate>Thu, 08 Feb 2007 14:58:11 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:f6bbd095-e878-4da6-bb7a-80522c41b489</guid>
      <link>http://blog.objectmentor.com/articles/2007/02/01/specs-vs-tests#comment-94</link>
    </item>
    <item>
      <title>"Specs vs. Tests" by Jason Gorman</title>
      <description>&lt;p&gt;Uncle Bob: In an interview I did with you for the (now defunct) objectmonkey.com site, didn&amp;#8217;t you tell me that you didn&amp;#8217;t care if TDD was like formal specification? Have you changed your mind since then?&lt;/p&gt;</description>
      <pubDate>Thu, 08 Feb 2007 09:08:18 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:8dc5431c-fadc-4ac5-a886-bf08b928ad3f</guid>
      <link>http://blog.objectmentor.com/articles/2007/02/01/specs-vs-tests#comment-93</link>
    </item>
    <item>
      <title>"Specs vs. Tests" by Steven Baker</title>
      <description>&lt;p&gt;Michael: RSpec includes a great mocking and stubbing framework (derived from SchMock, but now more closely resembling Mocha).&lt;/p&gt;


	&lt;p&gt;Myself, and many of the others working with RSpec hardly use state-based specifications at all.  I work almost exclusively with mocks in RSpec in most of the projects I use it.&lt;/p&gt;</description>
      <pubDate>Sun, 04 Feb 2007 12:36:11 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:18b07e78-53f3-4c80-aeaa-a05d1274417d</guid>
      <link>http://blog.objectmentor.com/articles/2007/02/01/specs-vs-tests#comment-80</link>
    </item>
    <item>
      <title>"Specs vs. Tests" by Paul Holser</title>
      <description>&lt;p&gt;I think the intent behind the &amp;#8220;One Assert Per Test&amp;#8221; rule was to get you thinking of &amp;#8220;TestCases&amp;#8221; as &amp;#8220;fixtures&amp;#8221; instead - wherein setUp() puts the system or a slice thereof in a specific state, and the testX() methods each contain one assertion about the state of the system that should hold if the system is working correctly.  So in the first code listing under &amp;#8220;The Demise of the &amp;#8216;One-Assert&amp;#8217; Rule&amp;#8221;, you&amp;#8217;d factor out the multiple rollMany(20,0) calls into a setUp() - just like the RSpec specification for the gutter game does.  It may very well be that the &amp;#8220;tests&amp;#8221; for a particular class, then, get spread out across many fixtures, instead of feeling like you have to place all tests for a given class in the same TestCase derivative.  The fixture names corresponding to system states, not classes.  At least that&amp;#8217;s how I&amp;#8217;ve read it.&lt;/p&gt;


	&lt;p&gt;It&amp;#8217;s interesting to see that RSpec facilitates thinking about aspects of the system being developed in terms of those system states, and not so much a one-spec-per-class mindset.&lt;/p&gt;</description>
      <pubDate>Fri, 02 Feb 2007 10:12:15 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:eb6b69db-6346-4d96-a624-e236b567e4a3</guid>
      <link>http://blog.objectmentor.com/articles/2007/02/01/specs-vs-tests#comment-75</link>
    </item>
    <item>
      <title>"Specs vs. Tests" by Paul Holser</title>
      <description>&lt;p&gt;I think the intent behind the &amp;#8220;One Assert Per Test&amp;#8221; rule was to get you thinking of &amp;#8220;TestCases&amp;#8221; as &amp;#8220;fixtures&amp;#8221; instead - wherein setUp() puts the system or a slice thereof in a specific state, and the testX() methods each contain one assertion about the state of the system that should hold if the system is working correctly.  So in the first code listing under &amp;#8220;The Demise of the &amp;#8216;One-Assert&amp;#8217; Rule&amp;#8221;, you&amp;#8217;d factor out the multiple rollMany(20,0) calls into a setUp() - just like the RSpec specification for the gutter game does.  It may very well be that the &amp;#8220;tests&amp;#8221; for a particular class, then, get spread out across many fixtures, instead of feeling like you have to place all tests for a given class in the same TestCase derivative.  The fixture names corresponding to system states, not classes.  At least that&amp;#8217;s how I&amp;#8217;ve read it.&lt;/p&gt;


	&lt;p&gt;It&amp;#8217;s interesting to see that RSpec facilitates thinking about aspects of the system being developed in terms of those system states, and not so much a one-spec-per-class mindset.&lt;/p&gt;</description>
      <pubDate>Fri, 02 Feb 2007 10:12:03 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:c822715d-4b19-444d-a016-8109061d4728</guid>
      <link>http://blog.objectmentor.com/articles/2007/02/01/specs-vs-tests#comment-74</link>
    </item>
    <item>
      <title>"Specs vs. Tests" by Michael Feathers</title>
      <description>&lt;p&gt;State is a slippery thing.  Uncle Bob&amp;#8217;s bowling game, as presented, is stateful, but imagine a different problem: you need to create a command object which accepts an array of throws and returns the score.  The object doesn&amp;#8217;t have mutable state, so technically no spec would alter state, but without a need for setup you could easily end up without all of the contexts that Bob found.&lt;/p&gt;


	&lt;p&gt;Seems that the benefit that BDD provides in this context lessens to the degree that you move toward less stateful objects, and that seems to happen among people who do a lot of interaction-style TDD.&lt;/p&gt;</description>
      <pubDate>Thu, 01 Feb 2007 12:02:56 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:0b71c59f-4a9f-4a6f-9fcb-a4e6e286284e</guid>
      <link>http://blog.objectmentor.com/articles/2007/02/01/specs-vs-tests#comment-73</link>
    </item>
    <item>
      <title>"Specs vs. Tests" by YAChris</title>
      <description>&lt;p&gt;Hmmmmmmmmmmm&amp;#8230;&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;m with you on the &amp;#8220;One Assert rule&amp;#8221;.  Recently, I&amp;#8217;ve been working on some code which is state-based, so I wind up with:&lt;/p&gt;


&lt;pre&gt;
State state = State.START;
state = stateMachine.step(state, 'Foo');
assertEquals(state, State.PAST_FOO);
state = stateMachine.step(state, 'Bar');
assertEquals(state, State.SKIP);
state = stateMachine.step(state, 'Baz');
assertEquals(state, State.PAST_FOO_AND_BAZ);
&lt;/pre&gt;

	&lt;p&gt;where it&amp;#8217;s necessary to have changing state, because the interesting bit is that there can be many SKIP situations interleaved, BUT we have to remember where we were before the SKIP, to wind up in the right state at the end.&lt;/p&gt;


	&lt;p&gt;So, the question is, in Specs-land would this have to become three complete Contexts?  That seems unfortunate.  Maybe there is a better way to do it?&lt;/p&gt;</description>
      <pubDate>Thu, 01 Feb 2007 11:36:21 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:c9635df7-af0c-4971-a009-9368199f5d35</guid>
      <link>http://blog.objectmentor.com/articles/2007/02/01/specs-vs-tests#comment-71</link>
    </item>
    <item>
      <title>"Specs vs. Tests" by Tobias Grimm</title>
      <description>&lt;p&gt;Recently I started using RSpec the first time. Beeing used to Assert.*, I&amp;#8217;ve always been sceptical about the &amp;#8220;pseudo-natural-languge-style&amp;#8221; expressions. I still don&amp;#8217;t see that much of a difference (from a programmers point of view) whether I write &amp;#8220;Assert.Equals(expected, actual)&amp;#8221; or &amp;#8220;actual.should == expected&amp;#8221;.&lt;/p&gt;


	&lt;p&gt;But what I absolutely love about RSpec, is the way it makes me think about what I want to code. In the &amp;#8220;normal&amp;#8221; TDD-way I&amp;#8217;ve always been kinda more focused on the design of my class currently under test, making me loose focus on what I really wanted to do. BDD forces me to focus more on application behaviour and helping me to stay on track. And just as you, I think, this is mainly because of the context/specification-style of writing tests.&lt;/p&gt;


	&lt;p&gt;With TDD the test method names often technically describe the class under test and how it is to be used. In BDD style the test methods (=specifications) describe what this part of my code is good for after all. The technical stuff then moves to the body of the specification.&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;ve now even started to write my NUnit-tests in BDD-style, which works pretty well. Each TestFixture is a context (defined in SetUp) and each Test is a specification. Of course this doesn&amp;#8217;t give me the nice failure messages that RSpec produces, but it seems to work too. I think David Chelimsky somwhere said something like &amp;#8220;BDD is doing TDD the right way.&amp;#8221;.&lt;/p&gt;


	&lt;p&gt;Regarding the &amp;#8220;Stable State&amp;#8221; thing &amp;#8211; I try to follow this rule, but sometimes it just doesn&amp;#8217;t seem to fit and I break it. Maybe a sign, that I haven&amp;#8217;t fully adopted the BDD-style yet.&lt;/p&gt;</description>
      <pubDate>Thu, 01 Feb 2007 09:52:12 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:af806ad6-a94c-41a6-9b2a-09d21d0eebf9</guid>
      <link>http://blog.objectmentor.com/articles/2007/02/01/specs-vs-tests#comment-69</link>
    </item>
    <item>
      <title>"Specs vs. Tests" by David Chelimsky</title>
      <description>&lt;p&gt;More thoughts &amp;#8211; the process through which specs evolve is going to have an impact on what goes in the name and what goes in the code. &amp;#8220;should score 0&amp;#8221; might have come from a customer who said &amp;#8220;this is what the score should be for an all gutter game&amp;#8221;. That name then serves as definition up front, documentation later.&lt;/p&gt;</description>
      <pubDate>Thu, 01 Feb 2007 09:31:13 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:65f1045f-cfbb-4b72-bf10-137d0fb93ec7</guid>
      <link>http://blog.objectmentor.com/articles/2007/02/01/specs-vs-tests#comment-68</link>
    </item>
    <item>
      <title>"Specs vs. Tests" by David Chelimsky</title>
      <description>&lt;p&gt;Chris &amp;#8211; that looks pretty interesting.&lt;/p&gt;


	&lt;p&gt;Michael &amp;#8211; while the duplication that you are describing is easy to end up with, there really is plenty of flexibility in what you write. You could, for example, say:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;A bowling game
- should score an all gutter game correctly
- should score an all ones game correctly
- should score an all spares game correctly
- should score an all strikes game correctly
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;or&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;A bowling game should produce the correct score
- given an all gutter game
- etc
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;One thing we&amp;#8217;re working on is some means of nesting contexts and/or specifications. So you could do something like this:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;Bowling game behaviour
- should score correctly
  - when game is all gutters
  - when game is all ones
  - when game is all spares
  - when game is all strikes
- should consider the game complete
  - after 20 0s
  - after 20 1s
  - after 21 5s
  - after 12 Xs
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Not saying these are &amp;#8220;right&amp;#8221;, just that they become possible. Note how the last example begins to feel more like a description of behaviour &amp;#8211; not just because of the word, but because of the nesting structure. An x should do y under conditions a,b,c and d.&lt;/p&gt;


	&lt;p&gt;Food for thought.&lt;/p&gt;</description>
      <pubDate>Thu, 01 Feb 2007 09:28:22 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:16c5d663-b47b-4a65-be04-e95fed56913f</guid>
      <link>http://blog.objectmentor.com/articles/2007/02/01/specs-vs-tests#comment-67</link>
    </item>
    <item>
      <title>"Specs vs. Tests" by Chris Hedgate</title>
      <description>&lt;p&gt;Michael: I do not know about RSpec (have just tried it once), but Specter (a similar framework for .Net) does that. Here is an example (Specter specs are written in Boo):&lt;/p&gt;


&lt;pre&gt;
context "When input is a phrase marked with asterisks":
  output as string

  setup:
    transformer = ContextileTransformer()
    output = transformer.ToHtml("*foo*")

  specify output.Must.Not.BeEmpty()

  specify "Phrase is given strength in output":
    output.Must.Equal("&amp;lt;p&amp;gt;&amp;lt;strong&amp;gt;foo&amp;lt;/strong&amp;gt;&amp;lt;/p&amp;gt;")
&lt;/pre&gt;

	&lt;p&gt;When this is run in a test runner the output is something like:&lt;/p&gt;


&lt;pre&gt;
WhenInputIsAPhraseMarkedWithAsterisks
  outputMustNotBeEmpty
  PhraseIsGivenStrength
&lt;/pre&gt;</description>
      <pubDate>Thu, 01 Feb 2007 09:11:43 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:03f16ad9-3d46-4e72-9711-59354574f4a4</guid>
      <link>http://blog.objectmentor.com/articles/2007/02/01/specs-vs-tests#comment-66</link>
    </item>
    <item>
      <title>"Specs vs. Tests" by Michael Feathers</title>
      <description>&lt;p&gt;One nit I have with BDD style is the fact that the test comment string so closely reflects the code in the case.  It feels like duplication.&lt;/p&gt;


	&lt;p&gt;When you&amp;#8217;ve worked to be able to say @g.score.should == 0, it feels weird to have to write &amp;#8220;score should be zero.&amp;#8221;  Granted, you write the string before the the code, but it still looks odd after the fact.  Makes you wonder whether a framework written in fluent style could generate the string.&lt;/p&gt;</description>
      <pubDate>Thu, 01 Feb 2007 07:52:24 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:0e8f724e-9eb7-4163-b9b8-534cbdb90e9a</guid>
      <link>http://blog.objectmentor.com/articles/2007/02/01/specs-vs-tests#comment-65</link>
    </item>
    <item>
      <title>"Specs vs. Tests" by Chris Hedgate</title>
      <description>&lt;p&gt;I really like the Stable State idea, and I think you are probably right about One Assert Per Test really was about this but did not quite get there. However, if you are using BDD and following Stable State, does not that more or less make One Assert Per Test a given as well? In your example above you have several specify, each with a single something.should specification. If they would all be slumped together in one specify, writing the string for that would again be impossible. And I think TDD can work the same way, which is why I have always tried to abide to One Assert Per Test. I recently wrote about this in &lt;a href="http://www.hedgate.net/articles/2006/11/17/one-assertion-per-test-should-come-natural" rel="nofollow"&gt;One Assert Per Test should come natural&lt;/a&gt;.&lt;/p&gt;</description>
      <pubDate>Wed, 31 Jan 2007 17:08:38 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:e1499f80-341b-4376-b5cc-3da1a1ebb76f</guid>
      <link>http://blog.objectmentor.com/articles/2007/02/01/specs-vs-tests#comment-63</link>
    </item>
  </channel>
</rss>
