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/
On May 30, 2005, at 10:05 PM, Craig Ambrose wrote:> > 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.I haven''t used Test::Unit::Mock but I have used Jim Weirich''s FlexMock package. I talk a little bit about it here: http://scott.elitists.net/users/scott/posts/mockery Not saying this is the "right" way to do mocks in Rails, but it''s working for me. -Scott _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Thanks Scott, I had a read of your article, and the FlexMock documentation. Looks good, but it doesn''t yet include some of the functionality that I''m after. I''m happy to code the extensions that I want for a mock object library, but with two different unit testing frameworks, and three different mock object libraries, I''m a little unsure which project to get involved in. Does anyone know which one is more widely used? or which one is more activelly developed? Craig On Tue, 31 May 2005 12:16 pm, Scott Barron wrote:> I haven''t used Test::Unit::Mock but I have used Jim Weirich''s FlexMock > package. I talk a little bit about it here: > http://scott.elitists.net/users/scott/posts/mockery-- Craig Ambrose Web Elements http://www.portallus.com/people/craigambrose/