c# - 在c#中运行单元测试时如何处理InvalidoperationException

标签 c# unit-testing moq

我使用 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/

相关文章:

c# - Moq 链接表达式导致参数计数不匹配

asp.net - ASP.NET HttpException.GetHttpCode()返回0

c# - 在带有 .NET MVC 的大型 CRUD View 中使用 IoC 和存储库模式

.net - 无法实例化类的代理。找不到无参数构造函数

c# - 除法时的小数舍入误差 (C#)

unit-testing - 使用嵌入式 Jetty 对 servlet 进行单元测试

ruby - 单元测试应该复制功能还是测试输出?

Angular 单元测试 : Using Generics to Create Component

c# - ASP.NET MVC Windows 身份验证显示登录提示而不是让我登录

c# - Mono:非 SSL URI 上的 HttpWebRequest SSL 错误