[英]Moq internal class and/or internal method
在我的項目中,我設置:
[assembly: InternalsVisibleTo("xyz")]
當我的 class 和方法公開時,我的單元測試工作。
public class MainController
{
public virtual void RunForm()
{
...
}
如果我更改為內部,我的單元測試將失敗。 這是起訂量的限制嗎?
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.
如錯誤消息中所述,您實際上需要將[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]
屬性添加到您的程序集中。 DynamicProxyGenAssembly2
是 Moq 在內部用於創建 class 的代理實例以覆蓋/實現虛擬/接口方法的程序集。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.