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
did i miss anything in code?
what's difference between stub and mock? [i read links didn't understand]?
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).
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.