Wrox Programmer Forums
|
BOOK: Professional Test Driven Development with C#: Developing Real World Applications with TDD
This is the forum to discuss the Wrox book Professional Test Driven Development with C#: Developing Real World Applications with TDD by James Bender, Jeff McWherter; ISBN: 978-0-470-64320-4
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional Test Driven Development with C#: Developing Real World Applications with TDD section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 3rd, 2013, 06:44 PM
Registered User
 
Join Date: Jun 2013
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation CH7: ItemRepositoryTests, valid item test is wrong

In Chapter 7 we write our first unit tests for the ItemTypeRepository. Either I am missing something or there is something wrong with the test for saving valid item types (then_a_valid_item_type_id_should_be_returned).
Look at the setup for this test:

Code:
public class and_saving_a_valid_item_type : when_working_with_the_item_type_repository
{
  private int _result;
  private ItemType _testItemType;
  private int _itemTypeId;

  protected override void Establish_context()
  {
     base.Establish_context();

     var randomNumberGenerator = new Random();
     _itemTypeId = randomNumberGenerator.Next(32000);

     _session.Setup(s => s.Save(_testItemType)).Returns(_itemTypeId);            
   }
   ...
The Save method on session is passed the _testItemType argument, but at this point _testItemType hasn't been initialized - it's null. Actually _testItemType is never initialized. Thus what the test is really saying is that when Save is called with null it should return a valid item id. In other words when Save is called with an invalid item it should work, which contradicts the test itself. The following production code would pass this test:

Code:
public int Save(ItemType itemType)
{
    int id;
    using (var session = sessionFactory.OpenSession())
    {
        id = (int)session.Save(null);
        session.Flush();
    }
    return id;
}
I've checked both the code in the book (p. 165) and the downloadable source code. Both has the problem.

This is a good example on the danger of using mocks. I don't know if the author mentions this in the book (I have only read up to ch. 7), but I believe it's an important point to make.

When using mocks you are basically telling the collaborators of the UUT how to behave. The catch is this: if you tell the collaborators to behave in a way that they wouldn't do in the real world it's the tests won't tell you this. In this case the issue is that the ISession collaborator is told to return a valid id when Save is called with null. On a real session object this would throw an exception (I hope), as hinted at by the other test in this class. Still, all the tests pass with flying colors, even on the clearly invalid implementation above. The sad thing is there are no easy solution to this problem. In this case integration tests would probably discover the bug, but in some cases it's not that easy. I've brought up the problem on the discussion group for another TDD book called "Growing Object Oriented Software - Guided by Tests". Some people there have come up with somewhat nice ways to minimize the problem (i.e. contract tests). Take a look if you're interested.

Regards,
Ole
 
Old June 29th, 2013, 02:46 AM
Registered User
 
Join Date: Jun 2013
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I think the reason is this; you don't care what is passed to the Save method of the object implementing the ISession interface, only that it is of type ItemType because you're not testing the Save method of NHibernate's ISession interface.

You're testing the Save method of the ItemTypeRepository. You've removed the dependency on NHibernate so you want it to do as little as possible (i.e. take whatever you give it and give you back something good in return). Hence the name of the test "and_saving_a_valid_item_type" which is really saying two things being: we assume we're getting a valid ItemType AND we assume we're getting a valid id back from NHibernate.

The code you're actually testing is mind blowingly simple, and that may be what's causing the confusion.

Code:
        public int Save(ItemType itemType)
        {
            int id;
            using (var session = _sessionFactory.OpenSession())
            {
                id = (int) session.Save(itemType); <-- we don't care what this                                                                                
                                                       does, just give us back a
                                                       good id and make sure the
                                                       code surrounding it does
                                                       what it should, in this case 
                                                       return the id.                
                session.Flush();
            }
            return id;
        }





Similar Threads
Thread Thread Starter Forum Replies Last Post
Wrong Required Item Type for variable.. ?? mphare XSLT 1 January 29th, 2009 06:06 PM
Navigation - ch7. wingri BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 4 April 8th, 2008 08:29 AM
VB Ch7 p247 Try It Out Using DetailsView Rich57 BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 0 January 31st, 2008 08:38 AM
CH7 - import package kizmit JSP Basics 4 June 16th, 2004 08:00 AM
Doing ch7 would u help me out? mara JSP Basics 1 November 18th, 2003 09:37 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.