1
interface ITaxi
{
    int Fare { get; set; }
    int getTotalFare();        
}


class Taxi : Car, ITaxi
{

    public Taxi(Engine e) : base (e){ }        

    public int Fare { get; set; }
    public int getTotalFare()
    {
        return 0;
    }
}


[TestFixture]
class TestTaxi
{
    [Test]
    public void TestTaxiFare()
    {
        MockRepository mockRepo = new MockRepository();
        ITaxi taxi = mockRepo.Stub<ITaxi>();
        using (mockRepo.Record())
        {
            SetupResult.For(taxi.getTotalFare()).Return(400);
        }
        Assert.AreEqual(400, taxi.getTotalFare());
    }
}

i'm new to Test Driven Development. i tried to mock a class and setuo a value for the method. but i

message castle.dynamicProxy.generators.generatorexception Type is not public, so a proxy cannot be generated. type: UnitTest.ITaxi
  1. did i miss anything in code?

  2. what's difference between stub and mock? [i read links didn't understand]?

Dineshkumar
  • 4,165
  • 5
  • 29
  • 43
  • A stub is a class that simply conforms to it's contract - ie, it will return the correct type values but that's it. A mock is a class that is "mocked" to perform some function and return specific values. – Dan Jan 20 '14 at 11:52
  • [Know Your Test Doubles](http://googletesting.blogspot.co.uk/2013/07/testing-on-toilet-know-your-test-doubles.html) – Chris Mantle Jan 20 '14 at 17:27

3 Answers3

2

You must declare the interface public for Rhino Mocks to be able to create a mock:

public interface ITaxi
{
    int Fare { get; set; }
    int getTotalFare();        
}

See also Access Modifiers in C#

Dennis Traub
  • 50,557
  • 7
  • 93
  • 108
0

change class to public

public class Taxi : Car, ITaxi

interface ITaxi

by default it isnot public

Daniel Rapaport
  • 375
  • 2
  • 18
0

As mentioned by the others you need to declare the interface as Public.

The difference between a mock and a stub is quite subtle (from my understanding of Roy Osherove's Art of Unit Testing book).

  • A stub is something that assists with the test but can never fail the test as the actual test is asserted against the Class Under Test.
  • A mock can fail a test as the assertion is performed against that object.

To maybe further explain, a stub is a canned response (a fake object) which assists with performing the assertion later (i.e. it can be a parameter to a method you are testing). You would never assert that a stub passes or fails a test as it's an object you have configured to assist in testing something else

A mock on the otherhand has expectations set, for example, if I configure this object with set of parameters what do I expect to happen? Will it change it's internal state (as I expect) or does it throw an exception (that I expect). This is the assertion you are looking to test.

In more complicated tests you could have many stubs but you should aim to only have one mock.

Damon
  • 3,004
  • 7
  • 24
  • 28