XUnit模拟方法,但不返回预期结果

时间:2019-05-24 06:30:53

标签: c# moq xunit

我在XUnit测试中使用了moq,但是由于某种原因,该模拟无法正常工作。这是我的单元测试:

 [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));
            }
        } 

这是实际的代码,我正在尝试测试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";
        }
 }

我试图单步执行单元测试中的代码,但是当断点在这一行时,requestString变量为null,而该变量应返回“样本字符串”。有人知道为什么吗?

string requestString = _constructRequestString.ConstructRequestString(request, client);

1 个答案:

答案 0 :(得分:2)

据我所知,您在嘲笑不正确: 您的模拟:

_mockConstructRequest.Setup(x => x.ConstructRequestString(searchRequestModel))
                                 .Returns("a sample string");

您正在调用的方法:

_constructRequestString.ConstructRequestString(request, client);

是不是应该这样:

_mockConstructRequest.Setup(x => x.ConstructRequestString(It.IsAny<Request>(),It.IsAny<HttpClient>()))
                                 .Returns("a sample string");

最重要的是:

尝试在构造函数中而不是在每个测试中初始化模拟和“ classUnderTest”,它将在测试之前每次运行,并为您做所有事情。例如:

public class UnitTestClass{

private readonly ClassUnderTest _classUnderTest;
private readonly Mock<ClassUnderTestDependecy> mockedInstance;

public UnitTestClass {
mockedInstance= new Mock<ClassUnderTestDependecy>();
_classUnderTest= new ClassUnderTest (ClassUnderTestDependecy.Object);
}

}