vendredi 25 octobre 2019

Integration tests with asp.net core (test of controllers without the views)

I am trying to setup a test project to test my controllers with identity and the database without having to define the views.

I have a unit test project where I can test my controller by instanciating it, passing the dbContext to the constructor.

 public class EventControllerTests
    {
        private readonly IEventRepository _eventRepository;
        private readonly EventController _controller;
        private readonly AppDbContext dbContext;

        const string cn = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=EventDb;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";

        public EventControllerTests()
        {

            var options = new DbContextOptionsBuilder<EVNTS.Web.Database.AppDbContext>()
                .UseSqlServer(cn).Options;
            dbContext = new EVNTS.Web.Database.AppDbContext(options);
            // Arrange
            _eventRepository = new EventRepository(dbContext);
            _controller = new EVNTS.Web.Controllers.EventController(_eventRepository);
        }

        [Fact]
        public void ActionIndexTest()
        {
            // Act
            var result = _controller.Index(1);

            // Assert
            var model = (Event)result.Model;
            Assert.Equal(1, model.Id);
        }
    }

I have an integration test project where I use a WebApplicationFactory

 public class BasicTests : IClassFixture<WebApplicationFactory<EVNTS.Startup>>
    {
        private readonly WebApplicationFactory<EVNTS.Startup> _factory;
        private readonly HttpClient _client;

        public BasicTests(WebApplicationFactory<EVNTS.Startup> factory)
        {
            _factory = factory;
            _client = _factory.CreateClient();
        }

        [Theory]
        [InlineData("/")]
        public async Task Get_EndpointsReturnSuccessAndCorrectContentType(string url)
        {

            // Act
            var response = await _client.GetAsync(url);

            // Assert
            response.EnsureSuccessStatusCode(); // Status Code 200-299
            Assert.Equal("text/html; charset=utf-8",
                response.Content.Headers.ContentType.ToString());
        }


        [Fact]

        public async Task TestUserRegistration()
        {
            var s = _factory.Services.GetRequiredService<EVNTS.Web.Repositories.IEventRepository>();
            var url = "/user/register";
            var inputModel = new EVNTS.Web.ViewModels.RegisterModel()
            {
                UserName = "eric",
                Password = "123456",
                ConfirmPassword = "123456"
            };
            var sObj = JsonSerializer.Serialize(inputModel);
            var content = new StringContent(sObj, Encoding.UTF8, "application/json");
            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            var response = await _client.PostAsync(url, content);
            var result = response.Content.ReadAsStringAsync();
        }

    }

The problem is that with the second option, the views have to be created and I need to use a library like AngleSharp to test the results.

I would like something in between where I can call the contructor directly and test the result view but with the DI injecting the UserManager and the dbContext for me.

any ideas?

Cheers

Aucun commentaire:

Enregistrer un commentaire