如何在单元测试中模拟Cookie

时间:2016-09-14 23:06:35

标签: c# asp.net-mvc unit-testing cookies nsubstitute

我有一个MVC控制器,我正在其中创建cookie并执行其余操作。我需要对这种方法进行单元测试。我不知道如何模仿饼干。

这是我在控制器中的方法:

public ActionResult Login(LogInRequest logInRequest)
{
    if (ModelState.IsValid)
    {
        HttpCookie usercookie = new HttpCookie("usercookie");
        usercookie["UserName"] = logInRequest.UserName;
        usercookie["Password"] = logInRequest.Password;
        usercookie.Expires = DateTime.Now.AddMinutes(10);
        **Response.Cookies.Add(usercookie);**
        return RedirectToAction("search", "LoanDriver");
        .......
        return View(logInRequest);
    }

在这行Response.Cookies.Add(usercookie);它给了我一个错误,因为Null。当我追踪cookie时有价值。

这是我的测试方法:

public void Web_Login_ShouldValidateUserAndLoginSuccessfully()
{
    using (var kernel = new NSubstituteMockingKernel())
    {
        // Setup the dependency incjection
        kernel.Load(new EntityFrameworkTestingNSubstituteModule());

        // Create test request data 
        var request = new LogInRequest { UserName = "test", Password = "test" };
        var fakeResponseHandler = new FakeResponseHandler();
        fakeResponseHandler.AddFakeResponse(new Uri("http://localhost/test"), new HttpResponseMessage(HttpStatusCode.OK));
        ConfigurationManager.AppSettings["SearchApiBaseUrl"] = "http://dev.loans.carfinance.com/internal";
        var server = new HttpServer(new HttpConfiguration(), fakeResponseHandler);
        var httpClient = new HttpClient(server);
        var controller = new LoanDriverController(httpClient);
        Fake.SetFakeContext(controller);
        var result = controller.Login(request);
        Assert.IsNotNull(result);
    }
}

我已经尝试将Cookie添加到Response中,但是没有工作并且得到同样的问题:

public static void SetFakeContext(this Controller controller)
{
    var httpContext = InitialiseFakeHttpContext();
    ControllerContext context = 
        new ControllerContext(new RequestContext(httpContext, new RouteData()), controller);
    controller.ControllerContext = context;
}

private static HttpContextBase InitialiseFakeHttpContext(string url = "")
{ 
    var HttpContextSub = Substitute.For<HttpContextBase>();
    var RequestSub = Substitute.For<HttpRequestBase>();
    var ResponseSub = Substitute.For<HttpResponseBase>();
    var serverUtilitySub = Substitute.For<HttpServerUtilityBase>();
    var itemsSub = Substitute.For<IDictionary>();
    HttpContextSub.Request.Returns(RequestSub);
    HttpContextSub.Response.Returns(ResponseSub);
    HttpContextSub.Server.Returns(serverUtilitySub);
    var cookie = Substitute.For<HttpResponseBase>();
    HttpContextSub.Response.Returns(cookie);
    return HttpContextSub;
}

1 个答案:

答案 0 :(得分:2)

我认为你可以通过在你的控制器中进行一些重构来处理这个问题,例如,你可以创建一个ICookieManager接口和一个CookieManager类(这个类包含添加cookie的逻辑,也实现了ICookieManager接口)然后你可以要么将此ICookieManager作为依赖项添加到构造函数中,要么作为属性添加,然后您将能够在单元测试中模拟此依赖项,如下所示:

let equals3 = (== • 3)

在您的控制器中:

public interface ICookieManager
    {
        void CreateUserCookie(string userName, string password, HttpResponseBase response, int expiration = 10);
    }

    public class CookieManager: ICookieManager
    {
        public void CreateUserCookie(string userName, string password, HttpResponseBase response, int expiration = 10)
        {
            HttpCookie usercookie = new HttpCookie("usercookie");
            usercookie["UserName"] = userName;
            usercookie["Password"] = password;
            usercookie.Expires = DateTime.Now.AddMinutes(expiration);
            response.Cookies.Add(usercookie);
        }
    }

在你的单元测试中:

 public class YourController : Controller
    {
        public ICookieManager CookieManager { get; set; }

        public YourController()
        {
            CookieManager = new CookieManager();
        }

        public YourController(ICookieManager cookieManager)
        {
            CookieManager = cookieManager;
        }

        public ActionResult Login(LogInRequest logInRequest)
        {
            if (ModelState.IsValid)
            {
                CookieManager.CreateUserCookie(logInRequest.UserName, logInRequest.Password, Response);
                return RedirectToAction("search", "LoanDriver");
            }
        }


    }