<?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"
	>
<channel>
	<title>Comments on: Mocking&#160;Ruby</title>
	<atom:link href="http://www.magpiebrain.com/blog/2005/06/07/mocking-ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.magpiebrain.com/blog/2005/06/07/mocking-ruby/</link>
	<description>Sam Newman's blog</description>
	<pubDate>Wed, 15 Oct 2008 19:51:10 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
		<item>
		<title>By: James Mead</title>
		<link>http://www.magpiebrain.com/blog/2005/06/07/mocking-ruby/#comment-2779</link>
		<dc:creator>James Mead</dc:creator>
		<pubDate>Sat, 26 Aug 2006 14:36:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.magpiebrain.com/2005/06/07/mocking-ruby/#comment-2779</guid>
		<description>Have you seen Mocha?</description>
		<content:encoded><![CDATA[<p>Have you seen Mocha?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jim Weirich</title>
		<link>http://www.magpiebrain.com/blog/2005/06/07/mocking-ruby/#comment-728</link>
		<dc:creator>Jim Weirich</dc:creator>
		<pubDate>Fri, 01 Jul 2005 15:20:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.magpiebrain.com/2005/06/07/mocking-ruby/#comment-728</guid>
		<description>Regarding eq() ... you can still ducktype without using an explicit eq() call.  Implement your constraint objects to respond to a mock_constraint method that just returns self.  Then in Kernel, define:

  def mock_constraint
    eq(self)
  end

No need for respond_to?, no need for explicit class checking.  The only concern is to make the mock_constraint command suitably unique to avoid name clashes.
</description>
		<content:encoded><![CDATA[<p>Regarding eq() ... you can still ducktype without using an explicit eq() call.  Implement your constraint objects to respond to a mock_constraint method that just returns self.  Then in Kernel, define:</p>
<p>  def mock_constraint<br />
    eq(self)<br />
  end</p>
<p>No need for respond_to?, no need for explicit class checking.  The only concern is to make the mock_constraint command suitably unique to avoid name clashes.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nat Pryce</title>
		<link>http://www.magpiebrain.com/blog/2005/06/07/mocking-ruby/#comment-727</link>
		<dc:creator>Nat Pryce</dc:creator>
		<pubDate>Wed, 08 Jun 2005 11:12:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.magpiebrain.com/2005/06/07/mocking-ruby/#comment-727</guid>
		<description>Why the eq call?  Because I'm a duck-typing nazi!  

How can the mock tell if an argument is a constraint or a normal object?  It can't without checking it's class hierarchy or callnig respond_to? on it.

Looking at the class hierarchy is bogus in a dynamically typed language:  the class hierarchy defines how the object is *implemented* and has no relation to its type.  The type is defined solely by how the object responds to messages.  So, it's perfectly valid to define a constraint object by writing an object that has matches? and describe_to methods (or whatever they'd be in Ruby).

Using respond_to is equally bogus.  I might use the method names matches? and describe_to? in other, unrelated, classes and  want to set up expectations in which those objects are expected arguments.  If the mock looked at the methods, it would incorrectly think that they were constraints, when they should instead be *wrapped* by constraints.

You may think that this is a bit academic, but I've been bitten by both problems when using Ruby libraries in real projects.

In a dynamically typed language you sometimes have to be more explicit about what you are passing around than you would in a statically typed language.  That's not a bad thing; it makes the code easier to read.</description>
		<content:encoded><![CDATA[<p>Why the eq call?  Because I&#8217;m a duck-typing nazi!  </p>
<p>How can the mock tell if an argument is a constraint or a normal object?  It can&#8217;t without checking it&#8217;s class hierarchy or callnig respond_to? on it.</p>
<p>Looking at the class hierarchy is bogus in a dynamically typed language:  the class hierarchy defines how the object is <strong>implemented</strong> and has no relation to its type.  The type is defined solely by how the object responds to messages.  So, it&#8217;s perfectly valid to define a constraint object by writing an object that has matches? and describe_to methods (or whatever they&#8217;d be in Ruby).</p>
<p>Using respond_to is equally bogus.  I might use the method names matches? and describe_to? in other, unrelated, classes and  want to set up expectations in which those objects are expected arguments.  If the mock looked at the methods, it would incorrectly think that they were constraints, when they should instead be <strong>wrapped</strong> by constraints.</p>
<p>You may think that this is a bit academic, but I&#8217;ve been bitten by both problems when using Ruby libraries in real projects.</p>
<p>In a dynamically typed language you sometimes have to be more explicit about what you are passing around than you would in a statically typed language.  That&#8217;s not a bad thing; it makes the code easier to read.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sam Newman</title>
		<link>http://www.magpiebrain.com/blog/2005/06/07/mocking-ruby/#comment-726</link>
		<dc:creator>Sam Newman</dc:creator>
		<pubDate>Wed, 08 Jun 2005 10:59:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.magpiebrain.com/2005/06/07/mocking-ruby/#comment-726</guid>
		<description>To be clear about my query concerning eq(), I think the equals constraint should be the default if possible. As for the number of times you expect the method to be called, I rarely use it but agree with Ade that in some cases it's useful. How about overloading expect to provide two forms, one assuming that it will be called once, so you can have:

expect {mock.do_something(10)}
expect {mock.do_something_else(10), 42}

expect 3..4 {mock.do_something(10)}</description>
		<content:encoded><![CDATA[<p>To be clear about my query concerning eq(), I think the equals constraint should be the default if possible. As for the number of times you expect the method to be called, I rarely use it but agree with Ade that in some cases it&#8217;s useful. How about overloading expect to provide two forms, one assuming that it will be called once, so you can have:</p>
<p>expect {mock.do_something(10)}<br />
expect {mock.do_something_else(10), 42}</p>
<p>expect 3..4 {mock.do_something(10)}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sam Newman</title>
		<link>http://www.magpiebrain.com/blog/2005/06/07/mocking-ruby/#comment-725</link>
		<dc:creator>Sam Newman</dc:creator>
		<pubDate>Wed, 08 Jun 2005 10:26:52 +0000</pubDate>
		<guid isPermaLink="false">http://www.magpiebrain.com/2005/06/07/mocking-ruby/#comment-725</guid>
		<description>That looks good - but why the need for the eq() call? And discussing this saturday would be good...</description>
		<content:encoded><![CDATA[<p>That looks good &#8211; but why the need for the eq() call? And discussing this saturday would be good&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nat Pryce</title>
		<link>http://www.magpiebrain.com/blog/2005/06/07/mocking-ruby/#comment-724</link>
		<dc:creator>Nat Pryce</dc:creator>
		<pubDate>Wed, 08 Jun 2005 10:16:34 +0000</pubDate>
		<guid isPermaLink="false">http://www.magpiebrain.com/2005/06/07/mocking-ruby/#comment-724</guid>
		<description>I have some really cool ideas about how to do Mocks in Ruby.  Here's what it would look like:

 mock = Mock.new "exampleMock"

 expect 1 { mock.do_something(eq(10), !null) }
 expect 3..4 { mock.something_else(eq("hello") }

 ... etc. etc...

I'll discuss how to do this on Saturday if you want.</description>
		<content:encoded><![CDATA[<p>I have some really cool ideas about how to do Mocks in Ruby.  Here&#8217;s what it would look like:</p>
<p> mock = Mock.new &#8220;exampleMock&#8221;</p>
<p> expect 1 { mock.do_something(eq(10), !null) }<br />
 expect 3..4 { mock.something_else(eq(&#8220;hello&#8221;) }</p>
<p> ... etc. etc&#8230;</p>
<p>I&#8217;ll discuss how to do this on Saturday if you want.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sam Newman</title>
		<link>http://www.magpiebrain.com/blog/2005/06/07/mocking-ruby/#comment-723</link>
		<dc:creator>Sam Newman</dc:creator>
		<pubDate>Wed, 08 Jun 2005 09:48:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.magpiebrain.com/2005/06/07/mocking-ruby/#comment-723</guid>
		<description>Firstly, the reason why I'd pass a class in to be mocked is so you can differentiate between methods which the object should be able to handle, and those it shouldn't - so throwing the normal method missing error.

Secondly I agree you don't need a proxy method to use method missing - the mock and the proxy can be the same thing. I was showing the .proxy call by way of showing a gradual move from JMock syntax/construct, to a more Ruby way of looking at it.

I have a problem with how messy Ruby syntax can end up looking, so seeing things like double underscores kind of makes me want to vomit. Sure, one you understand it it's easy to read, but before then it's a further barrier to entry (with lack of tool support, there are already enough of those).

MockIt is not readable to me at all, certainly not compared to JMock. Remember that the original idea was that JMock should read like a "proper sentence":http://nat.truemesh.com/archives/000188.html but the Java syntax got in the way - with Ruby needing less in the way of brackets I think there is a chance of making the expectations _more_ readable, not _less_. 

I do agree about once() though - I've never found a need to use twice()!</description>
		<content:encoded><![CDATA[<p>Firstly, the reason why I&#8217;d pass a class in to be mocked is so you can differentiate between methods which the object should be able to handle, and those it shouldn&#8217;t &#8211; so throwing the normal method missing error.</p>
<p>Secondly I agree you don&#8217;t need a proxy method to use method missing &#8211; the mock and the proxy can be the same thing. I was showing the .proxy call by way of showing a gradual move from JMock syntax/construct, to a more Ruby way of looking at it.</p>
<p>I have a problem with how messy Ruby syntax can end up looking, so seeing things like double underscores kind of makes me want to vomit. Sure, one you understand it it&#8217;s easy to read, but before then it&#8217;s a further barrier to entry (with lack of tool support, there are already enough of those).</p>
<p>MockIt is not readable to me at all, certainly not compared to JMock. Remember that the original idea was that JMock should read like a <a href="http://nat.truemesh.com/archives/000188.html" >proper sentence</a> but the Java syntax got in the way &#8211; with Ruby needing less in the way of brackets I think there is a chance of making the expectations <em>more</em> readable, not <em>less</em>. </p>
<p>I do agree about once() though &#8211; I&#8217;ve never found a need to use twice()!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aslak Hellesoy</title>
		<link>http://www.magpiebrain.com/blog/2005/06/07/mocking-ruby/#comment-722</link>
		<dc:creator>Aslak Hellesoy</dc:creator>
		<pubDate>Wed, 08 Jun 2005 01:58:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.magpiebrain.com/2005/06/07/mocking-ruby/#comment-722</guid>
		<description>Since ruby uses duck typing, creating a mock shouldn't require passing a class to the mock object's constructor.

Further, Ruby's method_missing wouldn't require you to have a proxy method on the mock to get the mocked instance. It can be the same object, thereby vastly improving its ease of use. 

This would require that the mock's "setup" methods don't clash with the mocked methods. This can be achieved with a naming convention, like prefixing "setup" methods with '__'.

This is the approach used in MockIt, a very small, but powerful mock library written by Jon Tirsen. You can find it here:

http://svn.damagecontrol.codehaus.org/trunk/rscm/test/rscm/mockit.rb
http://svn.damagecontrol.codehaus.org/trunk/rscm/test/rscm/mockit_test.rb

This mock implementation also hooks into Test::Unit's teardown in a similar way to JMock, so that mock.verify is not needed. This requires you to use include the MockIt module (mixin) in your test and use the new_mock method to create a mock. Example:

http://svn.damagecontrol.codehaus.org/trunk/damagecontrol/test/damagecontrol/project_test.rb?rev=1458&#38;view=auto

MockIt doesn't have the JMock feel that would allow you to use structures like once(), but its usage of blocks for constraints makes it very rubyish. Example:

foo = new_mock
foo.__expects(:bar).do &#124;param_1, param_2&#124;
  assert_equal("hi", param_1)
  assert_equal("there", param_1)
  "yo"
end
assert_equal("yo", foo.bar("hi", "there"))

This works really well for me. Imagine that we add support for JMock style invocation counts and possibly shorthand parameter constraints and invocation results such as:

foo = new_mock
foo.__expects(once(:bar)).with(eq("hi"), eq("there")).will(return_value("bar"))
assert_equal("yo", foo.bar("hi", "there"))

I'm not sure it would add that much to the readability. You get very far with blocks in Ruby and MockIt. I admit adding support for invocation counts such as once() could be implemented w/o sacrificing readability, but I rarely use other count qualifiers than once() anyway.

WDYT?</description>
		<content:encoded><![CDATA[<p>Since ruby uses duck typing, creating a mock shouldn&#8217;t require passing a class to the mock object&#8217;s constructor.</p>
<p>Further, Ruby&#8217;s method_missing wouldn&#8217;t require you to have a proxy method on the mock to get the mocked instance. It can be the same object, thereby vastly improving its ease of use. </p>
<p>This would require that the mock&#8217;s &#8220;setup&#8221; methods don&#8217;t clash with the mocked methods. This can be achieved with a naming convention, like prefixing &#8220;setup&#8221; methods with &#8217;__&#8217;.</p>
<p>This is the approach used in MockIt, a very small, but powerful mock library written by Jon Tirsen. You can find it here:</p>
<p><a href="http://svn.damagecontrol.codehaus.org/trunk/rscm/test/rscm/mockit.rb"  rel="nofollow">http://svn.damagecontrol.codehaus.org/trunk/rscm/test/rscm/mockit.rb</a><br />
<a href="http://svn.damagecontrol.codehaus.org/trunk/rscm/test/rscm/mockit_test.rb"  rel="nofollow">http://svn.damagecontrol.codehaus.org/trunk/rscm/test/rscm/mockit_test.rb</a></p>
<p>This mock implementation also hooks into Test::Unit&#8217;s teardown in a similar way to JMock, so that mock.verify is not needed. This requires you to use include the MockIt module (mixin) in your test and use the new_mock method to create a mock. Example:</p>
<p><a href="http://svn.damagecontrol.codehaus.org/trunk/damagecontrol/test/damagecontrol/project_test.rb?rev=1458&#038;view=auto"  rel="nofollow">http://svn.damagecontrol.codehaus.org/trunk/damagecontrol/test/damagecontrol/project_test.rb?rev=1458&#038;view=auto</a></p>
<p>MockIt doesn&#8217;t have the JMock feel that would allow you to use structures like once(), but its usage of blocks for constraints makes it very rubyish. Example:</p>
<p>foo = new_mock<br />
foo.__expects(:bar).do |param_1, param_2|</p>
<p>  assert_equal(&#8220;hi&#8221;, param_1)<br />
  assert_equal(&#8220;there&#8221;, param_1)<br />
  &#8220;yo&#8221;<br />
end<br />
assert_equal(&#8220;yo&#8221;, foo.bar(&#8220;hi&#8221;, &#8220;there&#8221;))</p>
<p>This works really well for me. Imagine that we add support for JMock style invocation counts and possibly shorthand parameter constraints and invocation results such as:</p>
<p>foo = new_mock<br />
foo.__expects(once(:bar)).with(eq(&#8220;hi&#8221;), eq(&#8220;there&#8221;)).will(return_value(&#8220;bar&#8221;))<br />
assert_equal(&#8220;yo&#8221;, foo.bar(&#8220;hi&#8221;, &#8220;there&#8221;))</p>
<p>I&#8217;m not sure it would add that much to the readability. You get very far with blocks in Ruby and MockIt. I admit adding support for invocation counts such as once() could be implemented w/o sacrificing readability, but I rarely use other count qualifiers than once() anyway.</p>
<p>WDYT?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sam Newman</title>
		<link>http://www.magpiebrain.com/blog/2005/06/07/mocking-ruby/#comment-721</link>
		<dc:creator>Sam Newman</dc:creator>
		<pubDate>Tue, 07 Jun 2005 22:28:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.magpiebrain.com/2005/06/07/mocking-ruby/#comment-721</guid>
		<description>Hmm - I'll have to think about that. I suppose it makes sense - the method name will be there all the time expect when stubbing, but both the argument list and the return values are optional (there would also need to be a :throws ) of course.

Much of the design behind JMock was there not only to make the expectations readable - which your code certainly is - but also to help the IDE create expectations using autocompletion (something that us Ruby programmers won't be having for a while I think).</description>
		<content:encoded><![CDATA[<p>Hmm &#8211; I&#8217;ll have to think about that. I suppose it makes sense &#8211; the method name will be there all the time expect when stubbing, but both the argument list and the return values are optional (there would also need to be a :throws ) of course.</p>
<p>Much of the design behind JMock was there not only to make the expectations readable &#8211; which your code certainly is &#8211; but also to help the IDE create expectations using autocompletion (something that us Ruby programmers won&#8217;t be having for a while I think).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Carlos Villela</title>
		<link>http://www.magpiebrain.com/blog/2005/06/07/mocking-ruby/#comment-720</link>
		<dc:creator>Carlos Villela</dc:creator>
		<pubDate>Tue, 07 Jun 2005 21:00:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.magpiebrain.com/2005/06/07/mocking-ruby/#comment-720</guid>
		<description>That'd be a great addition to the Ruby toolset, Sam. I'd like to recommend using hashes and named parameters instead of chained method calls:

mock_obj = Mock.new(ClassToMock)
mock_obj.expects(
  :do_stuff,
  :with =&#62; [arg1, arg2],
  :returns =&#62; someReturn
)



Kinda looks more like Ruby to me, but may go against some other JMock design principle I'm not aware of. What do you think?</description>
		<content:encoded><![CDATA[<p>That&#8217;d be a great addition to the Ruby toolset, Sam. I&#8217;d like to recommend using hashes and named parameters instead of chained method calls:</p>
<p>mock_obj = Mock.new(ClassToMock)<br />
mock_obj.expects(</p>
<p>  :do_stuff,<br />
  :with => [arg1, arg2],<br />
  :returns => someReturn<br />
)</p>
<p>Kinda looks more like Ruby to me, but may go against some other JMock design principle I&#8217;m not aware of. What do you think?</p>
]]></content:encoded>
	</item>
</channel>
</rss>
