简体   繁体   English

如何使用Moq对该动作进行单元测试?

[英]How can I unit test this action with Moq?

This action does several things, but I don't know where to start with Moq, newbie here. 这个动作可以做几件事,但是我不知道从这里开始的Moq,新手。

        [Authorization.AppAuthorize]
    public ActionResult Form()
    {
        // Create a new Supplier request form
        StoreRequestViewModel requestForm = new StoreRequestViewModel();
        requestForm.OwnerGuid = Guid.NewGuid();

        // Keep in mind the request form ids
        this.Session[requestForm.OwnerSessionCacheKey] = requestForm as IRequestFormViewModel;

        SetupContext();

        return View("IndexForm", requestForm);
    }

First up, you will need to mock the ControllerContext for this controller instance. 首先,您将需要为此控制器实例模拟ControllerContext That lets you set up the container for Session that you will need. 这样一来,您就可以为所需的Session设置容器。

Other than that, the code snippet is pretty testable already, assuming SetupContext doesnt do anything untoward. 除此之外,假设SetupContext不会做任何SetupContext事情,那么代码片段已经可以测试了。

You simply take the ActionResult , conver to ViewResult , and then check the ViewResult.ViewData.Model property (converting to StoreRequestViewModel ) and test the properties. 您只需将ActionResult转换为ViewResult ,然后检查ViewResult.ViewData.Model属性(转换为StoreRequestViewModel )并测试这些属性。

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

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