如何使用嵌套操作/回调对方法进行单元测试
How to unit test a method with a nested action / callback
我想测试一个发送 Web 请求并接收响应的方法。
然而,这不会直接发生,而是使用另一个 class 来构建请求并发送它。此外,HttpRequest
class 对从 "building class" 传递的响应使用回调,它是从我要测试的方法中获取的。
一些代码会使它更清楚。 (简体)
// this is the actual method I want to unit test
public void GetSomeDataFromTheWeb(Action<ResponseData> action, string data)
{
_webService.GetSomeDataFromTheWeb((req, resp) =>
{
// building the response depending on the HttpStatus etc
action(new ResponseData());
},data);
}
// this is the "builder method" from the _webService which I am gonna mock in my test
public void GetSomeDataFromTheWeb(Action<HTTPRequest, HTTPResponse> response, string data)
{
HTTPRequest request = new HTTPRequest(new Uri(someUrl)), HTTPMethods.Get,
(req, resp) =>
{
response(req, resp);
});
request.Send();
}
我可以创建一个 HttpResponse
它应该看起来的样子,但我不知道如何得到这个 "into" 最后一个方法的 response(req,resp)
调用。
我如何模拟 _webService
它从我要测试的方法中调用正确的回调 HttpResponse
我将输入我的单元测试?
基本上是这样的:
[Fact]
public void WebRequestTest()
{
var httpresponse = ResponseContainer.GetWebRequestResponse();
var webserviceMock = new Mock<IWebService>();
//get the response somehow into the mock
webserviceMock.Setup(w=>w.GetSomeDataFromTheWeb( /*no idea how*/));
var sut = new MyClassIWantToTest(webserviceMock);
ResponseData theResult = new ResponseData();
sut.GetSomeDataFromTheWeb(r=>{theResult = r}, "");
Assert.Equal(theResult, ResultContainer.WebRequest());
}
使用 It.IsAny
参数设置 GetSomeDataFromTheWeb
并使用设置中的 Callback
获取操作并使用存根调用它。
https://github.com/Moq/moq4/wiki/Quickstart#callbacks
webserviceMock
.Setup(w=>w.GetSomeDataFromTheWeb( It.IsAny<Action<HTTPRequest, HTTPResponse>>, It.IsAny<string>))
.Callback((Action<HTTPRequest, HTTPResponse> response, string data)=>{...});
我想测试一个发送 Web 请求并接收响应的方法。
然而,这不会直接发生,而是使用另一个 class 来构建请求并发送它。此外,HttpRequest
class 对从 "building class" 传递的响应使用回调,它是从我要测试的方法中获取的。
一些代码会使它更清楚。 (简体)
// this is the actual method I want to unit test
public void GetSomeDataFromTheWeb(Action<ResponseData> action, string data)
{
_webService.GetSomeDataFromTheWeb((req, resp) =>
{
// building the response depending on the HttpStatus etc
action(new ResponseData());
},data);
}
// this is the "builder method" from the _webService which I am gonna mock in my test
public void GetSomeDataFromTheWeb(Action<HTTPRequest, HTTPResponse> response, string data)
{
HTTPRequest request = new HTTPRequest(new Uri(someUrl)), HTTPMethods.Get,
(req, resp) =>
{
response(req, resp);
});
request.Send();
}
我可以创建一个 HttpResponse
它应该看起来的样子,但我不知道如何得到这个 "into" 最后一个方法的 response(req,resp)
调用。
我如何模拟 _webService
它从我要测试的方法中调用正确的回调 HttpResponse
我将输入我的单元测试?
基本上是这样的:
[Fact]
public void WebRequestTest()
{
var httpresponse = ResponseContainer.GetWebRequestResponse();
var webserviceMock = new Mock<IWebService>();
//get the response somehow into the mock
webserviceMock.Setup(w=>w.GetSomeDataFromTheWeb( /*no idea how*/));
var sut = new MyClassIWantToTest(webserviceMock);
ResponseData theResult = new ResponseData();
sut.GetSomeDataFromTheWeb(r=>{theResult = r}, "");
Assert.Equal(theResult, ResultContainer.WebRequest());
}
使用 It.IsAny
参数设置 GetSomeDataFromTheWeb
并使用设置中的 Callback
获取操作并使用存根调用它。
https://github.com/Moq/moq4/wiki/Quickstart#callbacks
webserviceMock
.Setup(w=>w.GetSomeDataFromTheWeb( It.IsAny<Action<HTTPRequest, HTTPResponse>>, It.IsAny<string>))
.Callback((Action<HTTPRequest, HTTPResponse> response, string data)=>{...});