Search code examples
c#unit-testingrhino-mocksinternalinternalsvisibleto

Cannot create a Mock class for an internal type using Rhino Mocks


I am using Rhino Mocks as a mocking framework for unit testing.

I have a class called Subject which is the class I want to test. It has a dependency on IStore.

IStore is defined as follows:

//internal interface : has InternalsVisible to both "Subject" 
//and "StoreTests" class namespaces
internal interface IStore {
    void Store(string name);
    //other methods
}

and the Subject class is defined as follows:

class Subject : IStore {
    private IStore internalStore;

    //constructor injection
    void Subject(IStore store) {
        internalStore = store;
    }

    void Store(string name) {
        internalStore.Store(name);
    }

    //other methods
}

My test class using RhinoMocks is as follows:

//test class
class StoreTests {
    Subject subject = new Subject();

    [Test]
    public StoreTest() {
        //Arrange
        var mockStore = MockRepository.GenerateMock<IStore>();
        string testName = "test";
        mockStore.Expect(x => x.Store(testName)).Returns(null);

        //Act
        subject.Store(testName);

        //Assert
        mockStore.VerifyAllExpectations();
    }

    //other test methods
}

In my setup, the interface is defined as internal and it has InternalsVisible set for both Subject class and StoreTests class. However, when the test case executes, it throws an exception at var mockStore = MockRepository.GenerateMock(); saying that IStore is not public and therefore it could not generate a Mock.

I think this is because the IStore is not public. However, since I have set InternalsVisibleTo on the IStore dll, will it not be sufficent for StoreTests to create a mock for that class?

Now I think this problem may be solved by making the IStore interface public. However given that this is not an option for me, is there any other way I can create a mock for IStore ?


Solution

  • Did you try making the assembly internals visible to Rhino mocks?

    [assembly: InternalsVisibleTo ("DynamicProxyGenAssembly2")]
    

    See Rhino Mocks Internal Members for details.

    When a class is mocked, a new class is generated at run-time which is derived from the mocked class. This generated class resides in a separate "temporary" assembly which is called "DynamicProxyGenAssembly2". So, the InternalsVisibleTo attribute needs to be set on the target assembly to allow access to its internal members from the temporary assembly; otherwise, the mock object can't override the internal member as it doesn't have access to it (which is also why the mocked method must be marked as virtual). Note that this is true even if the unit test and the tested class are in the same assembly.

    So, you need to make sure that the target class' assembly makes its internals visible to the proxy assembly as such (in AssemblyInfo.cs for example):