[英]XUnit mocking a method but doesn't return expected result
I am using moq in XUnit test but for some reason the mock is not working properly. 我在XUnit测试中使用了Moq,但由于某种原因,该模拟无法正常工作。 Here's my unit test: 这是我的单元测试:
[Fact]
public async Task SampleUnitTest()
{
//Arrange
var httpClient = new HttpClient(new FakeHttpMessageHandler());
_mockConstructRequest.Setup(x => x.ConstructRequestString(searchRequestModel))
.Returns("a sample string");
var service = new LibraryService(_mockConstructRequest.Object);
//Act
var response = service.GetResponse(new Request(), httpClient);
//Assert
response.Should().BeNull();
}
private class FakeHttpMessageHandler : HttpMessageHandler
{
public Func<HttpRequestMessage, CancellationToken, HttpResponseMessage> HttpRequestHandler { get; set; } =
(r, c) =>
new HttpResponseMessage
{
ReasonPhrase = r.RequestUri.AbsoluteUri,
StatusCode = HttpStatusCode.OK
};
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
return Task.FromResult(HttpRequestHandler(request, cancellationToken));
}
}
And here is the actual code and I am trying to test GetResponse
method. 这是实际的代码,我正在尝试测试GetResponse
方法。
public class LibraryService : ILibraryService
{
private IConfiguration _configuration;
private IRequestConstructor _requestContructor;
public LibraryService (IRequestConstructor requestConstructor)
{
_requestConstructor = requestConstructor;
}
public async Task<Response> GetResponse(Request request, HttpClient client)
{
//construct request
string requestString = _constructRequestString.ConstructRequestString(request, client);
return null;
}
}
public class RequestContructor : IRequestContructor
{
public string ConstructRequestString(Request request)
{
return "a request string";
}
}
I was trying to step through the code from my unit test but when the break point is at this line, requestString
variable is null while it is supposed to return "a sample string". 我试图从单元测试中逐步检查代码,但是当断点在这一行时, requestString
变量为null,而它应该返回“样本字符串”。 Anyone knows why? 有人知道为什么吗?
string requestString = _constructRequestString.ConstructRequestString(request, client);
As far as I can see you are mocking incorrectly: Your mock: 据我所知,您的模拟是错误的:模拟:
_mockConstructRequest.Setup(x => x.ConstructRequestString(searchRequestModel))
.Returns("a sample string");
Method you are calling: 您正在调用的方法:
_constructRequestString.ConstructRequestString(request, client);
Should not it be something like this: 不应该是这样的:
_mockConstructRequest.Setup(x => x.ConstructRequestString(It.IsAny<Request>(),It.IsAny<HttpClient>()))
.Returns("a sample string");
On top of that: 最重要的是:
Try to initialize your mocks and your "classUnderTest" in constructor instead of in each test, it will run each time before the test and will do everything for you. 尝试在构造函数中而不是在每个测试中初始化模拟和“ classUnderTest”,它将在测试之前每次运行,并为您做所有事情。 For example: 例如:
public class UnitTestClass{
private readonly ClassUnderTest _classUnderTest;
private readonly Mock<ClassUnderTestDependecy> mockedInstance;
public UnitTestClass {
mockedInstance= new Mock<ClassUnderTestDependecy>();
_classUnderTest= new ClassUnderTest (ClassUnderTestDependecy.Object);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.