gpt4 book ai didi

c# - 使用 Autofac 和 Moq 对 HttpResponseMessage 进行单元测试

转载 作者:太空宇宙 更新时间:2023-11-03 12:27:00 25 4
gpt4 key购买 nike

我在兜圈子。所以当我在调试中运行时,我的 WebApi 将它需要的输出返回给浏览器。 Firefox 和 IE 显示我需要的列表。但是,当我尝试使用 Mock 和 Autofac 对响应进行单元测试时,我返回的 HttpResponseMessage 没有内容。我感觉它在不同的上下文或类似的情况下返回。我不完全确定,因此问这个问题。我通过谷歌搜索和 Autofac 文档将以下单元测试串在一起。

WebApiMethod(包含在InScrapController中,_WebScrapSprocService由Autofac在构造函数中注入(inject))

public HttpResponseMessage GetFormItemsByFormNumber(int FormNumber)
{
HttpResponseMessage response;
try
{
//response = Request.CreateResponse(HttpStatusCode.OK, _WebScrapSprocService.GetFormItemsByFormNumber(FormNumber),new MediaTypeHeaderValue("application/json"));
response = Request.CreateResponse(HttpStatusCode.OK, new MediaTypeHeaderValue("application/json"));
response.Content = new StringContent(JsonConvert.SerializeObject(_WebScrapSprocService.GetFormItemsByFormNumber(FormNumber)),Encoding.UTF8, "application/json");
} catch (Exception e)
{
response = Request.CreateResponse(HttpStatusCode.InternalServerError, new StringContent(e.Message), new MediaTypeHeaderValue("application/json"));
}
//Checking if bob knows anything about this...
string bob = response.Content.ReadAsStringAsync().Result;
return response;
}

单元测试

public void GetFormItemsByFormNumber()
{
using (var mock = AutoMock.GetLoose())
{

var Service = mock.Mock<IWebScrapSprocService>().Setup(x => x.GetFormItemsByFormNumber(3392));
var service = mock.Create<InScrapController>();
service.Request = new HttpRequestMessage();
service.Request.SetConfiguration(new HttpConfiguration());
var HttpResponse = service.Request.CreateResponse(HttpStatusCode.OK, Service, new MediaTypeHeaderValue("application/json"));
var response = service.GetFormItemsByFormNumber(3392);
mock.Mock<IWebScrapSprocService>().Verify(x => x.GetFormItemsByFormNumber(3392));
Assert.AreEqual(HttpResponse, response);
}
}

最佳答案

这些 react 不会相同。还应该重构被测方法。您也不会取回任何内容,因为您还没有设置返回任何内容的服务。

public IHttpActionResult GetFormItemsByFormNumber(int FormNumber) {
IHttpActionResult response;
try {
var result = _WebScrapSprocService.GetFormItemsByFormNumber(FormNumber);
response = Ok(result);
} catch (Exception e) {
response = InternalServerError(e);
}
return response;
}

接下来更新测试

public void GetFormItemsByFormNumber() {
using (var mock = AutoMock.GetLoose()) {
// Arrange.
var formNumber = 3392;
var items = new List<FormItemsByFormNumber> {
new FormItemsByFormNumber {
//Populate as needed
},
new FormItemsByFormNumber {
//Populate as needed
},
//...etc
};
var serviceMock = mock.Mock<IWebScrapSprocService>();
serviceMock.Setup(x => x.GetFormItemsByFormNumber(formNumber)) // Calling this...
.Returns(items) // should return some value...
.Verifiable(); // and I want to verify that it was called.
var sut = mock.Create<InScrapController>();

// Act.
var response = sut.GetFormItemsByFormNumber(formNumber) as OkNegotiatedContentResult<List<FormItemsByFormNumber>>();

// Assert.
serviceMock.Verify(); //verify setups were exercised as expected.
Assert.IsNotNull(response);
}
}

关于c# - 使用 Autofac 和 Moq 对 HttpResponseMessage 进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44391969/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com