Rhino.Mocks: How to create stub of a class with different assembly fullname

542 Views Asked by At

I try to create two mock objects of a class but it creates two mocks with same assembly "FullName".

public class MyClassA : SomeAbstractClass, ISomeInterface1, ISomeInterface2
{
    public MyClassA(string someData) : base(someData)
    {
        //this property defined in base class
        CheckId = GetType().FullName;
    }

    internal override string CheckId { get; }
}

public class TestClass
{
    [Test]
    public void TestMethod()
    {
        var stub1 = new MockRepository().StrictMock<MyClassA>("some data 1");
        var stub2 = new MockRepository().StrictMock<MyClassA>("some data 2");

        Assert.AreEqual(stub1.CheckId, stub2.CheckId);
    }
}

Above assertion is true which I expected false.

I know I can create another interface that provides CheckId but I want to create same environment with production code.

So my question is why rhino mock produces same class name for different objects even if CheckId properties are different.

Is there any other way to create different mock instances of a same class without changing test logic?

If you need other details please let me know. Thanks in advance.

UPDATE: SOLVED

I found better solution than updating property manually. Making generic type of MyClassA solves the problem. As you can see below;

public class MyClassA<TDummy> : SomeAbstractClass, ISomeInterface1, ISomeInterface2
{
    public MyClassA(string someData) : base(someData)
    {
        //this property defined in base class
        CheckId = GetType().FullName;
    }

    internal override string CheckId { get; }
}

public class TestClass
{
    [Test]
    public void TestMethod()
    {
        var mock1 = MockRepository.GenerateMock<MyClassA<int>>("some data 1");
        var mock2 = MockRepository.GenerateMock<MyClassA<bool>>("some data 2");

        Assert.AreEqual(stub1.CheckId, stub2.CheckId);
    }
}

Because of different dummy types each generated mock are different instances of same base type. And after all assertion is false as I expected.

1

There are 1 best solutions below

1
On BEST ANSWER

One option would be to manually set the property so that it can be unique

public class MyClassA : SomeAbstractClass, ISomeInterface1, ISomeInterface2 {
    public MyClassA(string someData, string checkId) : base(someData) {
        //this property defined in base class
        CheckId = checkId;
    }

    internal override string CheckId { get; }
}

That way when mocking the classes you have control of setting that property

public class TestClass {
    [Test]
    public void TestMethod() {
        var stub1 = new MockRepository().StrictMock<MyClassA>("some data 1", "checkId 1");
        var stub2 = new MockRepository().StrictMock<MyClassA>("some data 2", "checkId 2");

        Assert.AreEqual(stub1.CheckId, stub2.CheckId); //Should fail.
    }
}

Otherwise if reflection is to be used you would be bettor off creating multiple classes.

public class MyClassA : SomeAbstractClass, ISomeInterface1, ISomeInterface2 {
    //...
}

public class MyClassB : SomeAbstractClass, ISomeInterface1, ISomeInterface2 {
    //...
}

public class TestClass {
    [Test]
    public void TestMethod() {
        var stub1 = new MockRepository().StrictMock<MyClassA>("some data 1");
        var stub2 = new MockRepository().StrictMock<MyClassB>("some data 2");

        Assert.AreEqual(stub1.CheckId, stub2.CheckId);
    }
}