Controller中的[TempData]在xUnit测试下引发异常:.NET Core 1.1 [英] [TempData] in Controller is throwing an exception under xUnit tests: .NET Core 1.1

查看:67
本文介绍了Controller中的[TempData]在xUnit测试下引发异常:.NET Core 1.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测试控制器是否正确处理异常。  我正在使用Visual Studio 2017,ASP.NET Core MVC,Net Core 1.1和xunit。  我在Visual Studio 2015下遇到了同样的问题(再次使用ASP.NET Core MVC)。


我可以在单独的项目中从xUnit测试访问和执行MVC控制器(同样的解决方案)  但是,我使用TempData [" message"]将一次性反馈消息传递给View ...成功或失败。


Controller运行正常,但是当它点击时设置TempData变量的行,它会异常终止。  如果我将控制器作为网站运行,一切都很好。


这几乎就像TempData是作为Session变量实现的,并且在xUnit测试中不可用。

解决方案

嗨daProf,


欢迎来到MSDN论坛。


根据您的描述,您获得了什么异常消息?如果你能更好地分享它有助于解决你的问题。


可以你在.Net Core 1.0中测试它,检查是否发生了同样的问题。如果它在.Net Core 1.0中是正常的,我想这可能是升级到.Net Core 1.1的问题。


请检查此帖子可能对您有所帮助:


https://stackoverflow.com/questions/41063934/xunit-tests-no-longer-discovered-in -net-core-1-1


希望能帮到你!


 


 


最好的问候,


Fletcher


I'm trying to test if controllers are handling exceptions correctly.  I'm using Visual Studio 2017, ASP.NET Core MVC, Net Core 1.1 and xunit.  I had the same problem under Visual Studio 2015 (again, using ASP.NET Core MVC).

I can access and execute an MVC Controller from an xUnit test in a separate Project (same solution).  However, I use TempData["message"] to pass one-time feedback messages to the View... success or fail.

The Controller runs OK, but when it hits the line setting the TempData variable, it abends.  If I run the controller as a web site, everything is fine.

It's almost as if TempData is implemented as a Session variable and is not available under xUnit testing.

解决方案

Hi daProf,

Welcome to MSDN forum.

According your description, what’s the exception message did you get? If you could share it better that help to solved your problem.

Could you test it in .Net Core 1.0, check to see if the same problem occured. If it’s normal in .Net Core 1.0, i guess it might be a problem with the upgrading to .Net Core 1.1.

Please check this thread might help you:

https://stackoverflow.com/questions/41063934/xunit-tests-no-longer-discovered-in-net-core-1-1

Hope to help you!

 

 

Best regards,

Fletcher


这篇关于Controller中的[TempData]在xUnit测试下引发异常:.NET Core 1.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆