views:

173

answers:

1

Hi All,

I need some practical examples of stubs and drivers with respect to top-down and bottom-up approaches to testing. I don't require code here. Just the scenario based examples.

A: 

A driver is a set of tests that test the interface of your class (methods, properties, constructor, etc).

A stub is a fake object that acts as a stand-in for other functionality like a database or a logger.

A mock is a fake object that has asserts in it.

Following is an example of a test using a mock object. If you take out the asserts, it becomes a stub. Collectively, these kinds of tests are drivers, because they exercise the methods and properties of your object.

Here is the example:

[Test]
public void TestGetSinglePersonWithValidId()
{
    // Tell that mock object when the "GetPerson" method is called to 
    // return a predefined Person
    personRepositoryMock.ExpectAndReturn("GetPersonById", onePerson, "1");
    PersonService service = new PersonService(
        (IPersonRepository) personRepositoryMock.MockInstance);
    Person p = service.GetPerson("1");
    Assert.IsNotNull(p);
    Assert.AreEqual(p.Id, "1");
}

http://www.zorched.net/2007/03/10/mocking-net-objects-with-nunit/

Robert Harvey
Can you please support your answer with some practical example?
Aditya