0

I was tasked to write unit/integration tests in the project. I am at the point that I need to write test methods for the controllers and every API endpoint. It's supposed to be an integration test for each endpoint. I have a few dependencies. The problem is that I'm unable to get the response of that web API call. For example, the login method(post) should return status 200 and JSON web token (The return type of the Login() method is Task<IActionResult>). The return code of that endpoint is:

return Ok(new
      {
          token = tokenHandler.WriteToken(token)
      });

My test class has Authentication repo, configuration, and auto mapper dependencies that I create in the test class constructor (I'm using Xunit). However, the returned result does not have anything I could use for checking the status code that has been returned or the type of object returned. Code below:

public class AuthenticationControllerTests
    {
        private readonly IConfiguration _applicationConfig;
        private readonly IAuthRepository _authenticationRepo;
        private readonly MapperConfiguration _mapperConfiguration;
        private readonly IMapper _mapperInstance;

        public AuthenticationControllerTests()
        {
            _applicationConfig = new ConfigurationBuilder()
                  .AddJsonFile("appsettings.json").Build();
            _authenticationRepo = new AuthenticationRepo(_applicationConfig);
            _mapperConfiguration = new MapperConfiguration(cfg => {
            cfg.CreateMap<OrderDto, Order>();
            cfg.CreateMap<User, UserDto>();
            });
            _mapperInstance = _mapperConfiguration.CreateMapper();
        }

        [Fact]
        public async void Login_ValidCreds_ReturnsOkWithJwt()
        {
            var controller = new AuthenticationController(_authenticationRepo, _applicationConfig, 
                             _mapperInstance);
            LoginDto userCreds = new LoginDto
            {
                Username = "username",
                Password = "password"
            };

            var result = await controller.Login(userCreds);

            Assert.Equal(HttpStatusCode.OK.ToString(), result.)
            
        }
    }

There is nothing in the result object that I could use for testing. How can I get the status code and returned object of this API call?

T4under4
  • 55
  • 1
  • 10
  • The thread you linked has answered my question. I can get the result properties and test against them. Thank you. – T4under4 Sep 23 '20 at 08:31

0 Answers0