我有一个带有类似构造函数的类:
public class SsoAuthenticationService : ISsoAuthenticationService
{
public SsoAuthenticationService(ILoginManager manager)
{
_manager = manager;
}
}
而且经理类也有这样的构造函数:
public class LoginManager:ILoginManager
{
private ILoginRepository login;
public LoginManager(ILoginRepository loginRepository)
{
login = loginRepository;
}
}
我正在尝试针对SSoAuthenticationService类编写单元测试
public void CreateLogin_Returns_Login()
{
//arrange
var mocManager = new Mock<ILoginManager>();
ILoginManager m = mocManager.Object;
//fails at the next line
var svc = new SsoAuthenticationService(mocManager.Object);
var request = new CreateSsoLoginRequest()
{
EmailAddress = "",
Password = "",
SecurityQuestionAnswer = "",
SecurityQuestionId = 0,
SiteIdentifier = "",
Username = ""
};
//act
var response = svc.CreateSsoLogin(request);
//assert
response.Should().NotBeNull();
}
当它尝试实例化SsoAuthenticationService
类("Value cannot be null.\r\nParameter name: value"
)时会出错,我相信问题是我还需要模拟IRepository
类,但我不确定模拟代码应该如何看待。
修改:完整的异常信息
'Progressive.Sso.WebServices.SsoAuthenticationService'的类型初始化程序引发了异常。
值不能为空。\ r \ n参数名称:值
在Progressive.Sso.WebServices.SsoAuthenticationService..ctor(ILoginManager manager)
at Progressive.Sso.Tests.Service_Methods.ServiceTest.CreateLogin_Returns_Login()在d:\ tfs \ sso \ Dev \ Dev \ Src \ SSO Web Services \ Progressive.Sso.Tests \ Service Methods \ ServiceTest.cs:第29行
任何人都可以帮助我吗?
答案 0 :(得分:0)
我找到了一个解决方案,问题与Moq或构造函数依赖关系无关。
我声明了以下两个成员变量
private static readonly bool _useOperationTimer = SsoConfig.UseServiceOperationTimer;
private static readonly bool _logRequestData = SsoConfig.LogRequestData;
静态方法调用是问题的原因,如果它们被注释掉,一切正常。