简体   繁体   English

如何在Asp.net Core 1中创建请求进行测试

[英]How to create request in Asp.net core 1 for testing

In my Asp.net core 1 app I have controller with following method: 在我的Asp.net core 1应用程序中,我具有使用以下方法的控制器:

[Microsoft.AspNetCore.Mvc.HttpPost()]
[Microsoft.AspNetCore.Mvc.RequireHttps]
public async System.Threading.Tasks.Task<Microsoft.AspNetCore.Mvc.IActionResult> Save()
{
    if (ModelState.IsValid)
    {
        try
        {
            var pass = Request.Form["password"].ToString();
            var pass1 = Request.Form["password1"].ToString();
            if (!pass.Equals(pass1))
            {
                return View("~/Views/PasswordRecovery.cshtml");
            }                  
        }
        catch (System.Exception ex)
        {
            return View("~/Views/Message.cshtml");
        }
    }
    return View("~/Views/Message.cshtml");
}

I want to write a test for this method. 我想为此方法编写一个测试。 So I have written this: 所以我写了这个:

[Xunit.Fact]
public async System.Threading.Tasks.Task SavePassNotEqualTest()
{
    var controller = new Controllers.PasswordRecoveryController(_mockRepo.Object);
    var dic = new System.Collections.Generic.Dictionary<string, Microsoft.Extensions.Primitives.StringValues>();
    dic.Add("password", "test");
    dic.Add("password1", "test1");
    var collection = new Microsoft.AspNetCore.Http.FormCollection(dic);
    controller.Request.Form = collection;  //request is null

    var result = await controller.Save();
    var viewResult = Xunit.Assert.IsType<Microsoft.AspNetCore.Mvc.ViewResult>(result);
    Xunit.Assert.Equal("~/Views/Message.cshtml", viewResult.ViewName);
}

The problem is that I need to set some test values to Form, Form is in Request, and Request is NULL. 问题是我需要将一些测试值设置为Form,Form在Request中,而Request为NULL。 I can not find, how can I create some not NULL request and fill it's Form with values. 我找不到,如何创建一些非NULL请求并用值填充它的Form。

EDIT Answers helped me to finish up with following solution: 编辑答案帮助我完成了以下解决方案:

I've created a method that will return a FormCollection: 我创建了一个将返回FormCollection的方法:

private Microsoft.AspNetCore.Http.FormCollection GetFormCollection()
{
    var dic = new System.Collections.Generic.Dictionary<string, Microsoft.Extensions.Primitives.StringValues>();
    dic.Add("password", "test");
    dic.Add("password1", "test1");
    return new Microsoft.AspNetCore.Http.FormCollection(dic);
}

And my test method is: 我的测试方法是:

[Xunit.Fact]
public async System.Threading.Tasks.Task SavePassNotEqualTest()
{           
    var controller = new Findufix.Controllers.PasswordRecoveryController(_mockRepo.Object);

    var httpContext = new Moq.Mock<Microsoft.AspNetCore.Http.HttpContext>();
    httpContext.Setup( x => x.Request.Form).Returns(GetFormCollection());  

    controller.ControllerContext.HttpContext = httpContext.Object;          

    var result = await controller.Save();
    var viewResult = Xunit.Assert.IsType<Microsoft.AspNetCore.Mvc.ViewResult>(result);
    Xunit.Assert.Equal("~/Views/PasswordRecovery.cshtml", viewResult.ViewName);
}

With Moq, you can do it like this: 使用Moq,您可以像这样:

var httpContext = new Mock<HttpContextBase>();           

httpContext.Setup(c => c.Request.Form).Returns(delegate()
{
    var formVaues = new NameValueCollection();
    formVaues .Add("Id", "123");
    formVaues .Add("Name", "Smith");

    return formVaues ;
});

If you pass a DefaultHttpContext to your controller, Request won't be null and you can assign the form to Request.Form . 如果将DefaultHttpContext传递给控制器​​,则Request不会为null,您可以将表单分配给Request.Form No mocking required. 无需嘲笑。

[Xunit.Fact]
public async System.Threading.Tasks.Task SavePassNotEqualTest()
{
    var controller = new Controllers.PasswordRecoveryController(_mockRepo.Object);
    var dic = new System.Collections.Generic.Dictionary<string, Microsoft.Extensions.Primitives.StringValues>();
    dic.Add("password", "test");
    dic.Add("password1", "test1");
    var collection = new Microsoft.AspNetCore.Http.FormCollection(dic);

    // Give the controller an HttpContext.
    controller.ControllerContext.HttpContext = new DefaultHttpContext();
    // Request is not null anymore.
    controller.Request.Form = collection;  

    var result = await controller.Save();
    var viewResult = Xunit.Assert.IsType<Microsoft.AspNetCore.Mvc.ViewResult>(result);
    Xunit.Assert.Equal("~/Views/Message.cshtml", viewResult.ViewName);
}

In Moq you can try to use Setup() or SetupGet() to teach it to return something that you need 在Moq中,您可以尝试使用Setup()或SetupGet()来教它返回所需的内容

something along the lines of 类似于

controller.SetupGet(x => x.Request.Form).Returns(collection);

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

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