How to set up an object with InternalsVisibleTo in an assembly to implement partial mocks with Rhino Mocks 3.6

851 views Asked by At

Below, I have code for an object that I would like to test. It is in an assembly called Business and I have added the attributes in the AssemblyInfo.cs to make internals visible to the test and rhino mocks which are located in another assembly. When testing the GenerateReport method, I can not fake out the call to ValidateWorkingDirectory when it is "internal" (System.ApplicationException : Must set Working Directory before any method calls.). If I make ValidateWorkingDirectory public, the problem goes away. I thought InternalsVisibleTo would address this issue.

public class MyClass : IMyClass
{
    private readonly IMyClassDataProvider _myClassDataProvider;

    public virtual string WorkingDirectory { get; set; }

    public MyClass(IMyClassDataProvider myClassDataProvider)
    {
        _myClassDataProvider = myClassDataProvider;
    }

    internal virtual void ValidateWorkingDirectory()
    {
        if (string.IsNullOrEmpty(WorkingDirectory))
        {
            throw new ApplicationException("Must set Working Directory before any method calls.");
        }
    }

    public virtual void GenerateReport(vars)
    {
        ValidateWorkingDirectory();
        InsertData(_myClassDataProvider.GetData(vars), "ReportName");
    }

    internal virtual void InsertData(DataSet analysis, string fileName)
    {
        DoSomeStuff();
    }

    private static void DoSomeStuff()
    {
        //Whatevs
    }
}

//In AssmeblyInfo.cs
[assembly: InternalsVisibleTo("UnitTests.Business")]
[assembly: InternalsVisibleTo("Rhino.Mocks")]


[TestFixture]
public class MyClassTests : TestFixtureBase
{
    private MockRepository _mocks;
    private IMyClassDataProvider  _myClassDataProvider;
    private MyClass _myClass;
    private var _vars;

    [SetUp]
    protected void Init()
    {
        _mocks = new MockRepository();
        _myClassDataProvider = _mocks.StrictMock<IMyClassDataProvider >();
        _myClass = _mocks.PartialMock<MyClass>(_myClassDataProvider);
        _vars = "who cares";
    }

    [Test]
    [ExpectedException(typeof(ApplicationException), ExpectedMessage = "Must set Working Directory before any method calls.")]
    public virtual void ShouldThrowAnExceptionIfWorkingDirectoryNotSet()
    {
        Expect.Call(_myClass.WorkingDirectory).Return(Random.Get<bool>() ? null : string.Empty);
        _mocks.ReplayAll();
        _myClass.ValidateWorkingDirectory();
        _mocks.VerifyAll();
    }

    [Test]
    public virtual void ShouldGenerateReport()
    {
        DataSet dataSetToReturn = new DataSet();
        using (_mocks.Ordered())
        {
            Expect.Call(() => _myClass.ValidateWorkingDirectory());
            Expect.Call(_myClassDataProvider.GetData(vars)).Return(dataSetToReturn);
            _myClass.InsertData(dataSetToReturn, "ReportName");
        }
        _mocks.ReplayAll();
        _myClass.GenerateReport(vars);
        _mocks.VerifyAll();
    }
}
1

There are 1 answers

0
k.m On BEST ANSWER

You need to expose your internal members to proxy assembly, not Rhino's assembly itself:

[assembly: InternalsVisibleTo ("DynamicProxyGenAssembly2")]

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.

This happens to be common misunderstanding, for detailed information on how to use internals visible with Rhino, check this documentation page.