我使用 MOQ 编写了单元测试。设置模拟时,我试图在 Linq 查询中创建类的对象。之后,我尝试运行单元测试,但收到以下错误消息。
"When called from 'VisitMemberInit', rewriting a node of type 'System.Linq.Expressions.NewExpression' must return a non-null value of the same type. Alternatively, override 'VisitMemberInit' and change it to not visit children of this type."
我写了如下代码-
_mockLdapAuthenticatorService.Setup(x => x.Authenticate(
new LoginRequest {
Username = It.IsAny<string>(),
Password = It.IsAny<string>() })).
Returns(new AuthenticationResult { Success = true });
最佳答案
更新设置。以下可能是您想要实现的目标。
_mockLdapAuthenticatorService
.Setup(x => x.Authenticate(It.IsAny<LoginRequest>()))
.Returns(new AuthenticationResult { Success = true });
关于c# - 在c#中运行单元测试时如何处理InvalidoperationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41362513/