简体   繁体   English

使用序列化viewresult的mvc3单元测试最佳实践

[英]mvc3 unit testing best practice using serialize viewresult

I am looking into the best ways to unit test my MVC 3 controllers. 我正在研究对MVC 3控制器进行单元测试的最佳方法。 I was thinking of taking the result of viewresult on executing the controller action with a bunch of different params, serializing it and saving to file as a base for future tests. 我正在考虑将viewresult的结果用于使用一系列不同的参数执行控制器操作,对其进行序列化并保存到文件中,以作为将来测试的基础。

2 questions: 2个问题:

  1. Is this a bad idea? 这是一个坏主意吗? for prior applications this would seem one of the safest ways to check that a change has not broken anything. 对于以前的应用程序,这似乎是检查更改没有破坏任何内容的最安全的方法之一。 I could deserialize my stored results, make any necessary changes and then compare to live results. 我可以反序列化存储的结果,进行必要的更改,然后与实时结果进行比较。
  2. If its a good way of testing, how can i serialize the viewresult? 如果这是一种很好的测试方法,我该如何序列化视图结果? In the code below i get an error that the ActionResult cannot be serialized. 在下面的代码中,我收到一个错误,提示ActionResult无法序列化。
//create viewresult to return to view
ActionResult viewResult = View(dv);

//save viewresult for future unit test comparisons.
//Save data as name of controller action and param value used
string fileName = logDir + "\\" + controllerActionName + tradeDate.ToString("MMddyyyy") + ".viewresult";

//serialze and save to file
System.IO.Stream stream = System.IO.File.Open(fileName,System.IO.FileMode.Create);
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
bFormatter.Serialize(stream, viewResult);
stream.Close();

//send viewresult to mvc3 view
return viewResult;

The easiest way to test your controller actions is to examine the view model. 测试控制器动作的最简单方法是检查视图模型。 You really should not need to be writing things out to files etc. 您确实不需要将内容写到文件等中。

YOu can just do something like 你可以做类似的事情

Given an action of: 采取以下行动:

public ViewResult AddNewDocument(int documentFolderId)
    {
        var documentFolder = documentFolderRepository.Get(documentFolderId);

        return View("AddNewDocument",
                    new AddNewDocumentView { DocumentFolderId = documentFolder.Id, DocumentFolderName = documentFolder.Name });
    }

Write a unit test of (in mspec though the same holds true of NUnit or MSTest: 编写一个单元测试(在mspec中,尽管NUnit或MSTest同样适用:

public class when_AddNewDocument_GET_is_requested : given_a_DocumentController
{
    Because of = () => result = documentController.AddNewDocument(documentFolderId);

    It should_return_a_view_result_with_the_view_name_AddDocument = () => result.ViewName.ShouldEqual("AddNewDocument");

    It should_have_a_view_model_of_type_AddNewDocumentView = () => result.ViewData.Model.ShouldBeOfType<AddNewDocumentView>();
    It should_have_return_document_folder_id_in_view_model = () => ((AddNewDocumentView)result.ViewData.Model).DocumentFolderId.ShouldEqual(documentFolderId);
    It should_have_return_document_folder_name_in_view_model = () => ((AddNewDocumentView)result.ViewData.Model).DocumentFolderName.ShouldEqual(documentFolderName);

    static ViewResult result;
}

The point is the viewmodel that you pass to the view contains all the data you need to test. 关键是传递给视图的视图模型包含需要测试的所有数据。 This can be grabbed directly from result.ViewData.Model. 可以直接从result.ViewData.Model中获取。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM