July 14, 2015 by Anuraj
.Net ASP.Net ASP.Net MVC Unit Testing
This post is about unit testing your middleware components. For testing middleware ASP.NET team introduced a TestHost package, this package contains a TestServer which can create an ASP.NET request processing pipeline and helpers to submit requests to the pipeline as if it is a real server. Requests made with these helpers never hit the network as they are processed directly in memory. . You can verify the content type, http status code etc. To write unit test, you require “Microsoft.AspNet.TestHost” package and dnx supported XUnit packages as well.
Here is the unit test for HelloWorld middleware component.
In this you are creating an instance of Test server, requesting for “/” url using client. And verifying the Http Status code. This is very minimal, here is the unit test for Html Minification middleware.
In this code, server is responding with a HTML content with whitespace, and verifying whether the middleware removes it successfully.
Here is the project.json file
Happy Programming :)
Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub