I am testing a method with FakeItEasy fake object. Fake object is a 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);
}
}
And this is code under the test:
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();
}
}
When I run test, it gives test exception => Expected: 2 But was: 0
I put break point in setup method and debug test. I see that Fake request GetResponse() method returns response with stream. That Length is 2.
But in test method, stream Length is 0.
Is there any settings about FakeItEasy? Or Where am I wrong?