【问题标题】:FakeItEasy Setup object is not returning in test methodFakeItEasy Setup 对象未在测试方法中返回
【发布时间】:2014-12-23 14:01:41
【问题描述】:

我正在测试一个带有 FakeItEasy 假对象的方法。假对象是一个MemoryStream

[TestFixture]
public class ServerClientTests
{
    private IWebClient webClient;
    private ServerClient serverClient;

    [SetUp]
    public void Setup()
    {
        webClient = A.Fake<IWebClient>();
        serverClient = new ServerClient(webClient);

        var responseStreamBytes = Encoding.Default.GetBytes("OK");
        var responseStream = new MemoryStream();
        responseStream.Write(responseStreamBytes, 0, responseStreamBytes.Length);
        responseStream.Seek(0, SeekOrigin.Begin);

        var response = A.Fake<WebResponse>();
        A.CallTo(() => response.GetResponseStream()).Returns(responseStream);

        var request = A.Fake<WebRequest>();
        A.CallTo(() => request.GetResponse()).Returns(response);

        A.CallTo(() => webClient.GetRequest("http://localhost:8080/myserver")).Returns(request);

    }

        [Test]
        public void Test1()
        {
            var result = serverClient.GetRequest("http://localhost/myserver");

            Assert.AreEqual(2, result.Length);
      }
  }

这是正在测试的代码:

public interface IWebClient
{
    WebRequest GetRequest(string url);
}

public class ServerClient
{
    private readonly IWebClient client;

    public ServerClient(IWebClient client)
    {
        this.client = client;
    }

    public Stream GetRequest(string url)
    {
        return client.GetRequest(url).GetResponse().GetResponseStream();
    }
}

当我运行测试时,它给出了测试异常 => 预期:2 但是是:0

我在设置方法和调试测试中设置了断点。我看到假请求 GetResponse() 方法返回带有流的响应。 长度是2。

但在测试方法中,流Length为0。

FakeItEasy 有设置吗?或者我哪里错了?

【问题讨论】:

标签: c# unit-testing mocking tdd fakeiteasy


【解决方案1】:

你正在设置

 A.CallTo(() => webClient.GetRequest("http://localhost:8080/myserver"))
                         .Returns(request);

然后调用

serverClient.GetRequest("http://localhost/myserver");

因此,webClient.GetRequest 被传递给 "http://localhost/myserver",这与它的期望 ("http://localhost8080/myserver") 不符,因此 webClient 返回一个它自己设计的假 MemoryStream,具有默认行为。

您可能希望使两个 URL 相同。或者,如果你想让你的假 webClient 不仅仅响应一个 URL,你可以使用更复杂的 argument matchers

将来,如果对为什么配置的方法没有按照您想要的方式运行而出现这种混淆,您可以考虑暂时使用对MustHaveHappened 的调用来检查 FakeItEasy 是否认为该方法已被调用。我们认为 FakeItEasy 的错误消息非常适合在这种情况下提供帮助。

在您的情况下,如果您添加了类似的测试

[Test]
public void Test2()
{
    var result = serverClient.GetRequest("http://localhost/myserver");

    A.CallTo(() => webClient.GetRequest("http://localhost:8080/myserver"))
        .MustHaveHappened();
}

它会说

Assertion failed for the following call:
  FakeItEasyQuestions.IWebClient.GetRequest("http://localhost:8080/myserver")
Expected to find it at least once but found it #0 times among the calls:
  1: FakeItEasyQuestions.IWebClient.GetRequest(url: "http://localhost/myserver")

【讨论】:

  • 但是我在构造函数中设置了 [serverClient = new ServerClient(webClient);] 所以 serverClient.GetRequest() 在类中调用 webClient。
  • 好的,这是我的参数问题。谢谢
猜你喜欢
  • 1970-01-01
  • 2015-02-11
  • 1970-01-01
  • 2018-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多