Reputation: 6805
On my project, I set:
[assembly: InternalsVisibleTo("xyz")]
My unit tests work when my class and method are public.
public class MainController
{
public virtual void RunForm()
{
...
}
If I change either to internal, my unit test fails. Is this a limitation of Moq?
var logger = new Mock<IExceptionLogger>();
var name = new Mock<MainController>
{
CallBase = true
};
name.Setup(x => x.RunForm()).Throws(new Exception()));
name.Object.Init(logger.Object);
logger.Verify(m => m.LogError(It.IsAny<Exception>(), Times.Once);
Exception:
Test method Tests.Controllers.MainControllerTest.ExceptionsInitGetLoggedToAppInsights threw exception:
System.ArgumentException: Cannot set up MainController.RunForm because it is not accessible to the proxy generator used by Moq:
Can not create proxy for method Void RunForm() because it or its declaring type is not accessible. Make it public, or internal and mark your assembly with [assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")] attribute, because assembly Abc is not strong-named.
Upvotes: 3
Views: 3645
Reputation: 4219
As stated in the error message, you actually need to add the [assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]
attribute to your assembly. DynamicProxyGenAssembly2
is the assembly that Moq uses internally to create the proxy instances of your class to override/implement virtual/interface methods.
Upvotes: 6