Learning ASP. NET Core 2.1: Using Webapplicationfactory in integration testing

Source: Internet
Author: User

Webapplicationfactory is a new feature of the ASP. NET Core 2.1 new features MVC functional test infrastructure, which encapsulates the use of testserver, allowing ASP. Integration testing is simpler and more flexible to configure.

Experience today in a real project and simply record it in this blog post.

Modify the. csproj project file where the integration test is located, and the SDK to be set to Microsoft.NET.Sdk.Web

<Sdk= "Microsoft.NET.Sdk.Web">

Add a NuGet package reference for MICROSOFT.ASPNETCORE.APP

<Include= "Microsoft.AspNetCore.App"/>

Add Microsoft.AspNetCore.Mvc.Testing's NuGet package reference, webapplicationfactory in it

<Include= "Microsoft.AspNetCore.Mvc.Testing"  Version= "2.1.0"  />

Integrated test class implements Iclassfixture<webapplicationfactory<startup>> interface

 Public class Webapitest:iclassfixture<webapplicationfactory<startup>>{}

This is webapplicationfactory, which enables the Iclassfixture interface to automatically depend on the type specified in the injected iclassfixture generic in the constructor of the test class.

With Webapplicationfactory, you can create a HttpClient instance directly with it, with the following code:

 Public class Webapitest:iclassfixture<webapplicationfactory<startup>>{    private  ReadOnly  HttpClient _httpclient;      Public Webapitest (webapplicationfactory<startup> Factory)    {        = factory. CreateClient ();    }}

This is the code that was not previously webapplicationfactory.

 Public class webapitest{    privatereadonly  HttpClient _httpclient;      Public webapitest ()    {        new webhostbuilder ( ). TestServer. Usestartup<startup>())            . CreateClient ();    }}

The simple example code above is just a small test sledgehammer, and Webapplicationfactory is more powerful by inheriting it to rewrite the Configurewebhost () method to configure the webhost

 Public class Customwebapplicationfactory<tstartup>     : webapplicationfactory<Startup>{     protected Override void configurewebhost (iwebhostbuilder builder)    {        = =        {            //...         });    }}

Learning ASP. NET Core 2.1: Using Webapplicationfactory in integration testing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.