简体   繁体   English

我应该模拟ControllerContext来对我的JsonNetResult数据进行单元测试

[英]Should I mock the ControllerContext to unit test my JsonNetResult data

I am using the JsonNetResult class from here in my asp.net mvc 4.0 project: 我在我的asp.net mvc 4.0项目中从这里使用JsonNetResult类:

http://james.newtonking.com/archive/2008/10/16/asp-net-mvc-and-json-net.aspx http://james.newtonking.com/archive/2008/10/16/asp-net-mvc-and-json-net.aspx

How can I unit test wether my c# classes are converted correctly to json format? 如何将我的C#类正确转换为json格式进行单元测试?

The Json conversion is executed in the ExecuteResult method which seem to be impossible to Json转换是在ExecuteResult方法中执行的,似乎无法

mock according to www. 根据www。嘲笑。

Some people even say this is too much mock effort just for the json testing. 甚至有人说,这仅仅是为了进行json测试而进行的大量模拟工作。

Some people say why not directly test the JsonConvert.SerializeObject(_data); 有人说为什么不直接测试JsonConvert.SerializeObject(_data); method. 方法。

I would be grateful to hear some professional advises about that topic. 听到一些有关该主题的专业建议,我将不胜感激。

If you want to test if your C# class can be serialized, then try serializing it directly, remember unit testing focuses on testing one particular operation, that link you provided does several things. 如果要测试您的C#类是否可以序列化,然后尝试直接对其进行序列化,请记住单元测试侧重于测试一个特定的操作,您提供的链接可以完成多项工作。 In order to unit test it you would have to create a method that does most of the work given the current state, rather than using implicit state. 为了对其进行单元测试,您必须创建一个在给定当前状态的情况下执行大部分工作的方法,而不是使用隐式状态。

To serialize it directly, just call the contents of if (Data != null) giving it the information you want to give. 要直接对其进行序列化,只需调用if (Data != null)的内容,即可为其提供要提供的信息。 Note that one piece of information you will need to give it is the stream to write to, since it writes to the Response directly. 请注意,您需要提供的一条信息是要写入的流,因为它直接写入Response

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

相关问题 如何正确模拟我的controllercontext来测试ViewResult.ExecuteResult()? - How can I correctly mock my controllercontext to test ViewResult.ExecuteResult()? 尝试创建 Mock.Of 时出错<controllercontext> () 用于 ASP.Net Core 3.1 单元测试</controllercontext> - Error trying to create Mock.Of<ControllerContext>() for ASP.Net Core 3.1 Unit Test 管理复杂的单元测试模拟数据 - Managing complex unit test mock data 如何为单元测试正确设置模拟数据? - How to properly setup mock data for unit test? 在单元测试期间渲染视图 - ControllerContext.DisplayMode - Render a View during a Unit Test - ControllerContext.DisplayMode 如何在单元测试中模拟控制器上下文,以便我对字符串函数的部分视图有效? - How do I mock controller context in my unit test so that my partial view to string function works? 我如何模拟Request.Url.GetLeftPart()以便我的单元测试通过 - How can I mock Request.Url.GetLeftPart() so my unit test passes 模拟 Redis 进行单元测试 - Mock Redis for unit test 用于单元测试的Mock HttpActionContext - Mock HttpActionContext for Unit Test 单元测试模拟 ControllerContext HttpContext 没有为类型“Microsoft.AspNetCore.Mvc.View...ITempDataDictionaryFactory”注册服务 - Unit Tests Mock ControllerContext HttpContext No service for type 'Microsoft.AspNetCore.Mvc.View...ITempDataDictionaryFactory' has been registered
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM