Is it possible to use the auto mocking framework provided in StructureMap.AutoMocking to test an internal virtual method within the CUT? I have already added InternalsVisibleTo to the appropriate AssemblyInfo.cs files. Here’s what I’m trying to do:
public ClassA {
public void Method1() { Method2(); }
internal virtual void Method2() { /* do work */ }
}
[Test]
public void TestClassA() {
// Arrange
var mockedClass = new RhinoAutoMocker<ClassA>();
mockedClass.PartialMockTheClassUnderTest();
mockedClass.ClassUnderTest.Expect(x => x.Method2());
// Act
mockedClass.ClassUnderTest.Method1();
// Assert
mockedClass.ClassUnderTest.VerifyAllExpectations();
}
I already know that changing the internal method to public works, but I don’t feel that this is an acceptable way to perform this test. Perhaps I am missing something fundamental about the way RhinoMocks and StructureMap interact but I figured based on other unit tests I’ve written that this type of thing should work.
Edit I suppose it would help to provide the error message I get when I try this:
System.InvalidOperationException : Invalid call, the last call has been used or no call
has been made (make sure that you are calling a virtual (C#) / Overridable (VB)
method).
at Rhino.Mocks.LastCall.GetOptions()
at Rhino.Mocks.RhinoMocksExtensions.Expect(T mock, Func`2 action)
at Rhino.Mocks.RhinoMocksExtensions.Expect(T mock, Action`1 action)
at ClassATest.TestClassA() in ClassA.cs
EDIT
I am wrong in my below statements. I finally circled back to this and used the keyword
protectedon all of my unit tests that were testinginternal virtualmethods within my class under test. This DOES work in structure map and is the only reason why I was receiving the error I was receiving.After a bit of research on this, there seems to be some elements of Rhino.Mocks that are not accessible via StructureMap.Automocker. When I re-worked my unit tests to make use of only the Rhino.Mocks framework, I was able to expose `internal virtual` methods to the test runner. Not an ideal situation and I would like to see support for this in newer versions of StructureMap, but that may be a long way off if there’s other more important things that precede adding support for `internal virtual` methods.
I re-worked my unit tests based on the following posts:
http://ayende.com/Wiki/Rhino+Mocks+-+Internal+Methods.ashx
How to mock protected virtual members with Rhino.Mocks?