如何解决OkNegotiatedContentResult <T>的子类中的重写ExecuteAsync方法未在单元测试中执行
我创建了OkNegotiatedContentResult<T>
的子类
public class SummarizeResult<T> : OkNegotiatedContentResult<T>
{
public SummarizeResult(T content,ApiController controller)
: base(content,controller) { }
public SummarizeResult(T content,IContentNegotiator contentNegotiator,HttpRequestMessage request,IEnumerable<MediaTypeFormatter> formatters)
: base(content,contentNegotiator,request,formatters) { }
public string Token { get; set; }
public override async Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
HttpResponseMessage response = await base.ExecuteAsync(cancellationToken);
var cookie = codeToCreateCookie();
response.Headers.AddCookies(new[] { cookie });
return response;
}
}
在我的控制器中,我有此代码可以使用
[HttpGet]
public async Task<IHttpActionResult> SomeMethod()
{
var result = new ApiResponse<bool> {Data = true};
return new SummarizeResult<ApiResponse<bool>>(content: result,controller: this)
{
Token = tokenService.CreateToken()
};
}
好消息:它有效。
坏消息是,public override async Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
从未在我的单元测试中调用
[TestMethod]
public async Task ShouldReturnCookie()
{
// Arrange
var controller = new AuthenticationController(tokenServiceMock.Object);
// Act
var result = await controller.SomeMethod();
// Assert
}
所以...当我想测试是否将cookie添加到响应中时,没有cookie。
因此ExecuteAsync
中的代码是在Web上下文中运行时执行的,而不是在单元测试上下文中执行的。
有人知道为什么会这样吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。