<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>[kumo.it] &#187; RSpec</title>
	<atom:link href="http://www.kumo.it/articles/category/blog/rspec/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kumo.it</link>
	<description></description>
	<lastBuildDate>Wed, 24 Aug 2011 21:38:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Designing a quiz with plain text user stories</title>
		<link>http://www.kumo.it/articles/2008/04/01/designing-a-quiz-with-user-stories/</link>
		<comments>http://www.kumo.it/articles/2008/04/01/designing-a-quiz-with-user-stories/#comments</comments>
		<pubDate>Mon, 31 Mar 2008 22:23:54 +0000</pubDate>
		<dc:creator>kumo</dc:creator>
				<category><![CDATA[notes]]></category>
		<category><![CDATA[quiz]]></category>
		<category><![CDATA[RSpec]]></category>
		<category><![CDATA[Stories]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.kumo.it/?p=78</guid>
		<description><![CDATA[Over the next couple of posts I thought that I would like to discuss the redevelopment of my hiragana and katakana quiz software. This software originally started out as a command-line Perl script, became a PHP web script, before turning into a Ruby on Rails web application. Unfortunately, although the development language changed and possibilities [...]]]></description>
			<content:encoded><![CDATA[<p>Over the next couple of posts I thought that I would like to discuss the redevelopment of my hiragana and katakana quiz software. This software originally started out as a command-line Perl script, became a PHP web script, before turning into a Ruby on Rails web application. Unfortunately, although the development language changed and possibilities increased, the overall functionality remained very much trapped in the original design. As I have started in other posts, I have plans for my quiz software, but it remains rather static, so perhaps it would be a good idea to talk about user stories as a way of providing clarity and focus.</p>
<p>I tend to prefer to use development tools that simplify the tasks that I have come to find routine, but perhaps more importantly provide me with a fresh and powerful way of looking at new problems that I need to solve. The latest tool to find me is &#8220;user stories&#8221;:http://rspec.info/documentation/stories.html with &#8220;RSpec&#8221;:http://rspec.info. Now, I must say that I was already using RSpec to &#8216;test&#8217; other Ruby on Rails applications and I think that everyone should try and play around with it for a bit, but user stories &#8212; in particular the plain-text flavour of them &#8212; are what currently occupy my notebook scribbles.</p>
<p>Let me show a somewhat simplistic and incomplete example of a user story:</p>
<pre lang="Story">
Story: starting a quiz
  As a visitor
  I want to start a quiz
  So that I can hopefully learn something

  Scenario: quiz doesn't exist
    Given a quiz named hiragana
    When I start another quiz
    Then I should see the 404 error page
    And I should see a link to the list of quizes

  Scenario: quiz does exist
    Given a quiz named hiragana
    When I start the quiz
    Then I should see the quiz
    And I should see a <a href='http://092.me'>question</a>
</pre>
<p>We have a story with two scenarios: when the quiz exists and when the quiz doesn&#8217;t exist. It is not very interesting, but at least we know that the user should see an error page if they try and start a quiz that doesn&#8217;t exist, and that they should also see a list of quizes that are available instead. If the quiz does exist then they should see a <a href='http://092.me'>question</a>.</p>
<p>This might be useful if we want to ensure that the 404 page is shown when the requested quiz doesn&#8217;t exist, but it is not very satisfactory to write down and seems a bit pointless. It also doesn&#8217;t help us understand what happens when a user starts a quiz.</p>
<p>One of the main thing that I like about plain text user stories is that I can easily scribble them down on a piece of paper. I can focus on describing my goals without worrying about the implementation details and afterwards even use this &#8220;pseudotestcode&#8221; directly. The plain text user story also is <a href='http://092.me'>nice</a>ly separated from the implementation code that is needed to make it work and because of this, it is easy to fit an understandable and detailed story in a manageable page or two.</p>
<p>I think I will use the following story in my quiz redevelopment and next time I will discuss other stories, and also how to test these stories.</p>
<pre lang="Story">
Story: starting a quiz
  As a visitor
  I want to start a quiz
  So that I can hopefully learn something

  Scenario: quiz doesn't exist
    Given a quiz named hiragana
    And a quiz named katakana
    When I start another quiz
    Then I should see the 404 error page
    And I should see a list of quizes
    And the list of quizes should have 2 items

  Scenario: default configuration
    Given a quiz named hiragana
    When I start the quiz
    Then I should see the quiz
    And I should see a <a href='http://092.me'>question</a>
    And I should see the text "Question 1 of 5"
    And there should be 3 textboxes

  Scenario: 10 <a href='http://092.me'>question</a>s
    Given a quiz named hiragana
    And the quiz has been configured for 10 <a href='http://092.me'>question</a>s
    When I start the quiz
    Then I should see the quiz
    And I should see "Question 1 of 10"

  Scenario: 5 hiragana per <a href='http://092.me'>question</a>
    Given a quiz named hiragana
    And the quiz has been configured for 5 hiragana
    When I start the quiz
    Then I should see the quiz
    And there should be 5 textboxes
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.kumo.it/articles/2008/04/01/designing-a-quiz-with-user-stories/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The perils in trusting syntax highlighting</title>
		<link>http://www.kumo.it/articles/2008/02/14/the-perils-in-trusting-syntax-highlighting/</link>
		<comments>http://www.kumo.it/articles/2008/02/14/the-perils-in-trusting-syntax-highlighting/#comments</comments>
		<pubDate>Thu, 14 Feb 2008 10:45:42 +0000</pubDate>
		<dc:creator>kumo</dc:creator>
				<category><![CDATA[notes]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[RSpec]]></category>
		<category><![CDATA[Stories]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.kumo.it/articles/2008/02/14/the-perils-in-trusting-syntax-highlighting/</guid>
		<description><![CDATA[Today after upgrading RSpec I ran a user story that I had just written and received a wonderful error: You have a nil object when you didn't expect it! (NoMethodError). The error occurred while evaluating nil.add_scenario. I didn&#8217;t even think to check my user story because the syntax highlighting looked fine and it made sense [...]]]></description>
			<content:encoded><![CDATA[<p>Today after upgrading <a href="http://rspec.info">RSpec</a> I ran a <a href="http://rspec.info/documentation/stories.html">user story</a> that I had just written and received a wonderful error: <code>You have a nil object when you didn't expect it! (NoMethodError). The error occurred while evaluating nil.add_scenario</code>. I didn&#8217;t even think to check my user story because the syntax highlighting looked fine and it made sense to me:</p>
<pre lang="Story">
  Scenario: areas have different types
    As an administrator
    I want to assign different types to the areas
    So that I can structure the home page

    Scenario: footer shows page links
      Given an area
      And area has 5 pages
      And area is of type footer
      When I view the home page
      Then I should see the home page
      And I should see the footer section
      And the list section should contain 0 areas
      And the footer section should contain 5 links
</pre>
<p>Of course, if I actually bothered to read it properly then I would have seen that the first line should have read:</p>
<pre lang="Story">
  Story: areas have different types
</pre>
<p>Moral of the story: even if the code that you have to write is small, always assume that you are capable of making silly mistakes!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kumo.it/articles/2008/02/14/the-perils-in-trusting-syntax-highlighting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>an assert_difference that works with arrays</title>
		<link>http://www.kumo.it/articles/2006/11/23/an-assert_difference-that-works-with-arrays/</link>
		<comments>http://www.kumo.it/articles/2006/11/23/an-assert_difference-that-works-with-arrays/#comments</comments>
		<pubDate>Thu, 23 Nov 2006 14:19:13 +0000</pubDate>
		<dc:creator>kumo</dc:creator>
				<category><![CDATA[notes]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[RSpec]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.kumo.it/articles/2006/11/23/an-assert_difference-that-works-with-arrays/</guid>
		<description><![CDATA[I have started using the better assert_difference that was posted at blog.caboo.se to improve my rails tests. This allowed me to write tests such as: assert_difference Item, :count do Item.create(:name => "Monkey magic") end This replaces: item_count = Item.count Item.create(:name => "Monkey magic") assert_difference, item_count + 1, Item.count but I also wanted to be able [...]]]></description>
			<content:encoded><![CDATA[<p>I have started using the <i>better assert_difference</i> that was posted at <a href="http://blog.caboo.se/articles/2006/06/13/a-better-assert_difference">blog.caboo.se</a> to improve my rails tests. This allowed me to write tests such as:</p>
<pre lang="rails">
  assert_difference Item, :count do
    Item.create(:name => "Monkey magic")
  end
</pre>
<p>This replaces:</p>
<pre lang="rails">
  item_count = Item.count
  Item.create(:name => "Monkey magic")
  assert_difference, item_count + 1, Item.count
</pre>
<p>but I also wanted to be able to test if emails were sent by writing:</p>
<pre lang="rails">
  assert_difference ActionMailer::Base.deliveries, :size do
    SystemNotifier.deliver_important_message
  end
</pre>
<p>That didn&#8217;t work and it was noted on that website that the code didn&#8217;t work properly with arrays so I have modified the assert_difference method to check if the objects respond to the method (e.g. <i>size</i>) and act accordingly:</p>
<pre line="1" lang="rails">
  def assert_difference(objects, method = nil, difference = 1)
    initial_values = []
    if objects.respond_to? method
      initial_values = objects.send(method)
    else
      objects = [objects].flatten
      initial_values = objects.inject([]) { |sum,obj| sum << obj.send(method) }
    end

    yield

    if difference.nil?
      objects.each_with_index { |obj,i|
        assert_not_equal initial_values[i], obj.send(method) #, "#{obj}##{method}"
      }
    else
      if objects.respond_to? method
        assert_equal initial_values + difference, objects.send(method) #, "#{objects}##{method}"
      else
        objects.each_with_index { |obj,i|
          assert_equal initial_values[i] + difference, obj.send(method) #, "#{obj}##{method}"
        }
      end
    end
  end
</pre>
<p>I have tested it for an array such as <em>ActionMailer::Base.deliveries</em> but I haven't tested it in other situations so hopefully it won't create any problems.<br />
<hr />The rspec version of this is:</p>
<pre line="1" lang="rails">
  def assert_difference(objects, method = nil, difference = 1)
    initial_values = []
    if objects.respond_to? method
      initial_values = objects.send(method)
    else
      objects = [objects].flatten
      initial_values = objects.inject([]) { |sum,obj| sum << obj.send(method) }
    end

    yield

    if difference.nil?
      objects.each_with_index { |obj,i|
        initial_values[i].should_not_equal obj.send(method) #, "#{obj}##{method}"
      }
    else
      if objects.respond_to? method
        (initial_values + difference).should_eql objects.send(method) #, "#{objects}##{method}"
      else
        objects.each_with_index { |obj,i|
          (initial_values[i] + difference).should_eql obj.send(method) #, "#{obj}##{method}"
        }
      end
    end
  end
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.kumo.it/articles/2006/11/23/an-assert_difference-that-works-with-arrays/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced (User agent is rejected)
Database Caching 9/17 queries in 0.036 seconds using disk: basic

Served from: www.kumo.it @ 2012-02-04 20:27:02 -->
