在我的项目中,我设置了:
如果我更改其中一个为internal,我的单元测试会失败。这是Moq的限制吗?
[assembly: InternalsVisibleTo("xyz")]
当我的类和方法是public时,我的单元测试可以正常工作。
public class MainController
{
public virtual void RunForm()
{
...
}
如果我更改其中一个为internal,我的单元测试会失败。这是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);
异常:
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.
.csproj
文件中设置设置,而不是使用汇编指令:https://www.meziantou.net/declaring-internalsvisibleto-in-the-csproj.htm - silkfiremark your assembly with [assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")] attribute
- devNull