title: Integration tests with asp.net core (test of controllers without the views)
body: <p>I am trying to setup a test project to test my controllers with identity and the database without having to define the views.</p>
  
  <p>I have a unit test project where I can test my controller by instanciating it, passing the dbContext to the constructor.</p>
  
  <pre class="lang-cs prettyprint-override"><code> 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&lt;EVNTS.Web.Database.AppDbContext&gt;()
                  .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);
          }
      }
  </code></pre>
  
  <p>I have an integration test project where I use a WebApplicationFactory</p>
  
  <pre><code> public class BasicTests : IClassFixture&lt;WebApplicationFactory&lt;EVNTS.Startup&gt;&gt;
      {
          private readonly WebApplicationFactory&lt;EVNTS.Startup&gt; _factory;
          private readonly HttpClient _client;
  
          public BasicTests(WebApplicationFactory&lt;EVNTS.Startup&gt; 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&lt;EVNTS.Web.Repositories.IEventRepository&gt;();
              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();
          }
  
      }
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>any ideas?</p>
  
  <p>Cheers</p>
  
  <p>Here is the controller:</p>
  
  <pre><code>public class UserController : Controller
      {
          private readonly UserManager&lt;User&gt; _userManager;
          public UserController(UserManager&lt;User&gt; userManager)
          {
              _userManager = userManager;
          }
  
          [HttpPost]
          public async Task&lt;IActionResult&gt; Register([FromBody] RegisterModel model)
          {
              IdentityResult? result=null;
  
              if (ModelState.IsValid)
              {
                  var user = await _userManager.FindByNameAsync(model.UserName);
                  if (user == null)
                  {
                      user = new User
                      {
                          Id = Guid.NewGuid(),
                          UserName = model.UserName,
                      };
                      result = await _userManager.CreateAsync(user, model.Password);
                  }
              }
              return View(result);
          }
      }
  </code></pre>