<?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: Testing in Rails: Part 2 - Unit Testing Ruby Classes</title>
	<atom:link href="http://www.nullislove.com/2007/11/17/testing-in-rails-part-2-unit-testing-ruby-classes/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.nullislove.com/2007/11/17/testing-in-rails-part-2-unit-testing-ruby-classes/</link>
	<description>Code for Coders</description>
	<pubDate>Fri, 12 Mar 2010 21:14:08 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Kevin Skoglund</title>
		<link>http://www.nullislove.com/2007/11/17/testing-in-rails-part-2-unit-testing-ruby-classes/comment-page-1/#comment-914</link>
		<dc:creator>Kevin Skoglund</dc:creator>
		<pubDate>Sat, 26 Jan 2008 14:52:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.nullislove.com/2007/11/17/testing-in-rails-part-2-unit-testing-ruby-classes/#comment-914</guid>
		<description>Jeff, I made a mistake.  My code and my blog post weren't identical.  Thanks for catching it.  I fixed it in the tutorial now.

Change the initialize method in your model to be:

def initialize(options={})
  self.model = options[:model] &#124;&#124; 'Volvo'
  self.year = options[:year] &#124;&#124; 2007
  self.color = options[:color] &#124;&#124; 'unknown'
end

Notice the addition of the curly braces after 'options'.  The initialize method had one argument declared and was giving you an error if you didn't send an argument.  Now, if no argument is sent, it will not give an error, but instead set options equal to an empty hash.  In Ruby it is always true that the number of arguments sent to a method must match the number declared, unless you provide default values to fall back on.

On the bright side, now you see firsthand how testing can identify problems!  :-P</description>
		<content:encoded><![CDATA[<p>Jeff, I made a mistake.  My code and my blog post weren&#8217;t identical.  Thanks for catching it.  I fixed it in the tutorial now.</p>
<p>Change the initialize method in your model to be:</p>
<p>def initialize(options={})<br />
  self.model = options[:model] || &#8216;Volvo&#8217;<br />
  self.year = options[:year] || 2007<br />
  self.color = options[:color] || &#8216;unknown&#8217;<br />
end</p>
<p>Notice the addition of the curly braces after &#8216;options&#8217;.  The initialize method had one argument declared and was giving you an error if you didn&#8217;t send an argument.  Now, if no argument is sent, it will not give an error, but instead set options equal to an empty hash.  In Ruby it is always true that the number of arguments sent to a method must match the number declared, unless you provide default values to fall back on.</p>
<p>On the bright side, now you see firsthand how testing can identify problems!  <img src='http://www.nullislove.com/wp-includes/images/smilies/icon_razz.gif' alt=':-P' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeff Schoolcraft</title>
		<link>http://www.nullislove.com/2007/11/17/testing-in-rails-part-2-unit-testing-ruby-classes/comment-page-1/#comment-913</link>
		<dc:creator>Jeff Schoolcraft</dc:creator>
		<pubDate>Sat, 26 Jan 2008 11:34:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.nullislove.com/2007/11/17/testing-in-rails-part-2-unit-testing-ruby-classes/#comment-913</guid>
		<description>I've been following along, and it's been a fun series.  I sat down to start writing out your examples and I guess I'm missing something because I'm a ruby neophyte.

in your test_initialize, when you do default_car = Car.new and don't pass it the hash, that doesn't work for me.

I get an ArgumentException:

  1) Error:
test_initialize(CarTest):
ArgumentError: wrong number of arguments (0 for 1)
    car_test.rb:34:in `initialize'
    car_test.rb:34:in `new'
    car_test.rb:34:in `test_initialize'

I'm set up almost identically to your example:

class Car
  attr_accessor :model, :year, :color

  def initialize(options)
    self.model = options[:model] &#124;&#124; 'Volvo'
    self.year  = (options[:year] &#124;&#124; 2008).to_i
    self.color = options[:color] &#124;&#124; 'unknown'
  end
  ...
end

and CarTest:

 def test_initialize
[34]    default_car = Car.new
    assert_equal('volvo', default_car.model)
    assert_equal(2008, default_car.year)
    assert_equal('unknown', default_car.color)
    ...
  end

Line [34] is marked.

my ruby version: ruby 1.8.6 (2007-06-07 patchlevel 36) [i486-linux]

Everything else works as expected, as long as I'm passing in some hash.  What is it that I'm not getting?</description>
		<content:encoded><![CDATA[<p>I&#8217;ve been following along, and it&#8217;s been a fun series.  I sat down to start writing out your examples and I guess I&#8217;m missing something because I&#8217;m a ruby neophyte.</p>
<p>in your test_initialize, when you do default_car = Car.new and don&#8217;t pass it the hash, that doesn&#8217;t work for me.</p>
<p>I get an ArgumentException:</p>
<p>  1) Error:<br />
test_initialize(CarTest):<br />
ArgumentError: wrong number of arguments (0 for 1)<br />
    car_test.rb:34:in `initialize&#8217;<br />
    car_test.rb:34:in `new&#8217;<br />
    car_test.rb:34:in `test_initialize&#8217;</p>
<p>I&#8217;m set up almost identically to your example:</p>
<p>class Car<br />
  attr_accessor :model, :year, :color</p>
<p>  def initialize(options)<br />
    self.model = options[:model] || &#8216;Volvo&#8217;<br />
    self.year  = (options[:year] || 2008).to_i<br />
    self.color = options[:color] || &#8216;unknown&#8217;<br />
  end<br />
  &#8230;<br />
end</p>
<p>and CarTest:</p>
<p> def test_initialize<br />
[34]    default_car = Car.new<br />
    assert_equal(&#8217;volvo&#8217;, default_car.model)<br />
    assert_equal(2008, default_car.year)<br />
    assert_equal(&#8217;unknown&#8217;, default_car.color)<br />
    &#8230;<br />
  end</p>
<p>Line [34] is marked.</p>
<p>my ruby version: ruby 1.8.6 (2007-06-07 patchlevel 36) [i486-linux]</p>
<p>Everything else works as expected, as long as I&#8217;m passing in some hash.  What is it that I&#8217;m not getting?</p>
]]></content:encoded>
	</item>
</channel>
</rss>
