dotnet-practitioner
dotnet-practitioner

Reputation: 14148

Correct way to nunit test httpcontext object for asp.net

I have been able to successfully unit test my asp.net related methods with context object by following hanselman article and using FakeHttpContext.

I have been told that constructing FakeHttpContext as follows and setting up QueryString, Server Variables inside FakeHttpContext as follows is not real test for asp.net context object. This method and this test is working well for me as provided in the hanselman article.

    public static HttpContextBase FakeHttpContext()
    {
        var context = new Mock<HttpContextBase>();
        var request = new Mock<HttpRequestBase>();
        var response = new Mock<HttpResponseBase>();
        var session = new Mock<HttpSessionStateBase>();
        var server = new Mock<HttpServerUtilityBase>();

        request.Setup(x => x.QueryString).Returns(new NameValueCollection
                {
                    {"blah","7"}, 
                    {"blah1","8"}
                });

        request.Setup(x => x.ServerVariables).Returns(new NameValueCollection
                {
                    {"SERVER_NAME","myserver"}, {"SCRIPT_NAME","myperfectscript"}, 
                    {"SERVER_PORT","80"}, {"HTTPS","www.microsoft.com"}
                });

        request.Setup(x => x.Form).Returns(new NameValueCollection
                {
                    {"TextBox1", "hello"},
                    {"Button1", "world"},
                    {"Label1", "yournamehere"}
                });

        request.Setup(x => x.Cookies).Returns(new HttpCookieCollection());

        HttpCookie cookie1 = new HttpCookie("userInfo");
        cookie1["username"] = "Tom";
        cookie1["password"] = "pass";

        request.Object.Cookies.Add(cookie1);

        HttpCookie cookie2 = new HttpCookie("compInfo");
        cookie2["companyname"] = "google";
        cookie2["companypassword"] = "googlepassword111";

        request.Object.Cookies.Add(cookie2);

        context.Setup(ctx => ctx.Request).Returns(request.Object);
        context.Setup(ctx => ctx.Response).Returns(response.Object);
        context.Setup(ctx => ctx.Session).Returns(session.Object);
        context.Setup(ctx => ctx.Server).Returns(server.Object);

        return context.Object;
    }

I am told to capture all the page details with all the response/request level parameters in a file via fiddler, read the file into the test object, read page level parameters such as query string from the saved file and then test against it.

This approach of using file/fiddler does not make sense to me. It will be just an extra exercise of writing lot of code to read and regex the file.

Do you agree with me? What have you done in this situation?

Upvotes: 1

Views: 1426

Answers (1)

Pete
Pete

Reputation: 539

I'd normally declare tests that are accessing any resources like files, databases, streams, etc more an integration test than a unit test. If you're running these tests as part of a bigger continous integration environment these tests can fail easily and you get a red build just because the file was locked for example...

Upvotes: 1

Related Questions