简体   繁体   English

测试来自 IActionResult 控制器方法的结果,当返回是一个元组值时

[英]Testing the results from an IActionResult controller method, when the return is a Tuple value

I've got a controller method that returns this to the client我有一个控制器方法可以将它返回给客户端

return Ok(new { user, profile});

It used to return the user object on its own, so my unit test on the method looked like this它曾经自己返回用户对象,所以我对该方法的单元测试看起来像这样

var res = await _controller.Get(_userId);
var okResult = res as OkObjectResult;
var resUser = okResult.Value as User;

i could then test individule bits of my return然后我可以测试我返回的各个部分

Assert.Contains("user@domain.com", resUser.EmailAddress);

However, now i'm returning aa tuple object our of the controller method its not working.但是,现在我要返回一个元组对象,我们的控制器方法不起作用。 Ive tried doing我试过做

var resUser1 = okResult.Value as Tuple<User, Profile>;

But that always returns me null.但这总是让我返回 null。 Any idea how i can convert my result from the controller into something i can look into now its not just a flat object?知道如何将我的结果从控制器转换成我现在可以查看的东西,它不仅仅是一个平面对象吗?

Thanks谢谢

好的,所以我尝试使用 ValueTuple 来转换结果,但它仍然接缝带回了一个空对象,所以我最终只是为结果创建了一个真实的对象并转换到该对象工作正常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单元测试返回 IActionResult 的控制器方法 - Unit testing controller methods which return IActionResult 当 controller 返回 UI 并返回完整结果时,在后台运行线程 - 从 Controller 方法异步调用 function - Running a thread in the background when controller return to UI with full results - async call of a function from Controller method 从 URL 返回文件作为 IActionResult - Return File from URL as IActionResult 我为什么要返回任务<iactionresult>在 Controller 中?</iactionresult> - Why should I return Task<IActionResult> in a Controller? 无法从返回类型为“Microsoft.AspNetCore.Mvc.IActionResult”的操作方法返回 null - Cannot return null from an action method with a return type of 'Microsoft.AspNetCore.Mvc.IActionResult' 单元测试从另一个方法内部调用的方法的返回值 - Unit Testing return value of method called from inside another method 与AJAX一起传递时,在IActionResult中获取空值 - Getting a null value in IActionResult when passed with AJAX 当 controller 在另一个项目中时,Get 方法不返回值 - Get method doesn`t return value when controller is in another project 如何在变量 C# 中捕获 IActionResult 方法返回的状态代码 - How to capture the status code of an IActionResult Method return in a variable C# Controller 在运行 xUnit 测试时返回 null IActionResult - Controller returns a null IActionResult when running xUnit test
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM