Hi folks,
While perhaps this is a general ruby issue, rather than a rails issue, people
doing agile development with rails have no doubt done a fair bit of work with
Test::Unit and mock objects. I''ve read the excellent testing howto on
the
rails site, and the testing chapter of the beta Agile Development with Rails
book, but neither really cover mock objects.
Rails has a mock objects directory though. What is this for? How do people use
mock objects?
I''ve been having a look at the Test::Unit::Mock library for ruby. Does
anyone
here have any experience with this? Below is a specific question about this
library, I''d welcome any general thoughts on mocking in ruby/rails, or
specific thoughts on the example below.
------
Having read the Test::Unit::Mock docs, I understand how to use it to make a
mock object which ''does'' something (what I call an
''actor''), but I''m trying
to figure out how to use mock objects which check expectations (what I can a
''critic'').
For example, I have a test as follows
def test_move_forward_asks_collection_to_move_it_forward
collection = Test::Unit::MockObject(PageCollection).new
page = Page.new
page.collection = collection
page.move_forward
collection.activate
collection.setReturnValues(:move_forward => nil)
collection.verify
end
I''m trying to test that the when page.move_forward is called, it calls
collection.move_forward. Ideally I''d also like to test that it passes
through
a certain parameter (being itself).
I know how to write this test manually, but it means extending the page class,
and involves far too many lines of code. I presume your library can go part
way to helping with this, but I don''t quite get it.
With SimpleTest for PHP, I can write a test for this as follows:
Mock::generate(''Collection'');
function test_move_forward_asks_collection_to_move_it_forward
{
$page = new Page;
$collection = new MockCollection;
$collection->expectArguments(''move_forward'',
array($page));
$collection->expectOnce(''move_forward'');
$page->setCollection($collection);
$page->move_forward();
$collection->verify();
}
kind regards,
Craig
--
Craig Ambrose
Web Elements
http://www.portallus.com/people/craigambrose/