Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 767
Santhosh Avatar asked Jul 28 '11 07:07

Santhosh


3 Answers

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):

like image 114
alun Avatar answered Nov 10 '22 12:11

alun


Yep it should be enough to add following in the AssemblyInfo.cs file of an assembly under the test:

[assembly: InternalsVisibleTo("Tests.Assembly.Name")]
[assembly: InternalsVisibleTo("NUnit.Framework")]
[assembly: InternalsVisibleTo("Rhino.Mocks, PublicKey=00240000048000009400000006020000002400005253413100040000010001009D1CF4B75B7218B141AC64C15450141B1E5F41F6A302AC717AB9761FA6AE2C3EE0C354C22D0A60AC59DE41FA285D572E7CF33C320AA7FF877E2B7DA1792FCC6AA4EB0B4D8294A2F74CB14D03FB9B091F751D6DC49E626D74601692C99EAB7718ED76A40C36D39AF842BE378B677E6E4EAE973F643D7065241AD86ECC156D81AB")]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]
like image 28
sll Avatar answered Nov 10 '22 10:11

sll


Well this might be answered, but for me it didn't work .

So here's what i did to make it work (might help others, and even me, on the next project....):

In Tool menu in Visual studio : External tools : Add for name i put "LongStrongName" , but put whatever you feel fit :

(this path, or wherever the sn.exe is for you):

Command:
  C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\sn.exe
Arguments:
  -Tp $(TargetPath)

(Click the checkbox , output to "Use output Window")

Now you can click on the project, then go to tools and go to the "LongStrongName" menu:

and VS will output :

Public key is       0240000048000009400000006020000002400005253413100040000010001009badbe86c32ec0
ec429f0b3909*********

Public key token is 6ccc051********

Open the assembly.cs and add:

[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]

add whatever assemblies you need , and voila (i had to put multiple assemblies).

like image 3
Fx Mzt Avatar answered Nov 10 '22 10:11

Fx Mzt