writing xUnit test for asp.net core MVC web application not WEB API application

时间:2019-05-22 18:38:12

标签: asp.net asp.net-core xunit xunit.net

Need to write the xUnit.net code to test my mvc web application controller actions with return type view (Not the Web API service application.), when I search in the web (google, bing) found only web api controller actions.

thing is need to mock the Http Context request and response and check my action method logic executed correctly and return correct view.

when I am did this I got issue when adding sessions/reading session values.

using below code I am able to did the web API application

public class AdminIntegrationTest : IClassFixture<WebApplicationFactory<Startup>>
{
    private readonly WebApplicationFactory<Startup> _factory;


    public AdminIntegartionTest(WebApplicationFactory<Startup> fixture)
    {
        _factory = fixture;
    }

    [Fact]
    public async Task TestGetAllBusinessSubUnit2Async()
    {
        // Arrange
        var request = "/api/Admin/GetAllBusinessSubUnit2";
        var client = _factory.CreateClient();
        // Act
        var response = await client.GetAsync(request);
        // Assert
        var okResult = response.Content.ReadAsStringAsync();
        //Assert.IsType<OkObjectResult>(okResult);
        Assert.NotNull(okResult);
    //  var responseStrong = await response.Content.ReadAsStringAsync();
    }
}

using same I am not able to test the web application.

is it any other way to do this?

0 个答案:

没有答案