如何解决我无法在单元/集成测试中获得Web api调用结果
我的任务是在项目中编写单元/集成测试。我现在需要为控制器和每个API端点编写测试方法。它应该是每个端点的集成测试。我有一些依赖。问题是我无法获得该Web API调用的响应。例如,login方法(post)应返回状态200和JSON Web令牌(Login()方法的返回类型为Task
return Ok(new
{
token = tokenHandler.WriteToken(token)
});
我的测试类具有在测试类构造函数中创建的身份验证存储库,配置和自动映射器依赖项(我正在使用Xunit)。但是,返回的结果没有任何可用于检查已返回的状态码或返回的对象类型的内容。下面的代码:
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.)
}
}
结果对象中没有任何可用于测试的东西。如何获取此API调用的状态代码和返回的对象?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。