Question

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]?

Était-ce utile?

La solution 2

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.

Autres conseils

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#

change class to public

public class Taxi : Car, ITaxi

interface ITaxi

by default it isnot public

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top