lundi 29 février 2016

Change mocked getResponse .net

I have a test that get some devices states from an endpoint. I have that response mocked and get the info correctly. The test that I want to implement must get this info and after a while get that info again and check if it's different to throw an event. The problem is that I can't change the mocked response in my test. This is what I tried.

Implementation of mocked WebRequest.

class TestWebRequestCreate : IWebRequestCreate
{

    public TestWebRequest testWebRequest { get; set; }

    public WebRequest Create(Uri uri)
    {
        WebRequest webRequest;
        if (testWebRequest == null)
        {
            webRequest = new TestWebRequest(HttpStatusCode.OK, "Testing response");
        }
        else
        {
            webRequest = testWebRequest;
        }
        return webRequest;
    }
}

class TestWebRequest : WebRequest
{
    public HttpStatusCode httpStatusCode { get; set; }
    public Stream responseMessage;

    /// <summary>
    ///  Initialize a new instance of <see cref="TestWebRequest"/>
    ///  with the response to return
    /// </summary>
    public TestWebRequest(HttpStatusCode httpStatusCode, string responseMessage)
    {
        this.httpStatusCode = httpStatusCode;
        this.responseMessage = StreamFromString(responseMessage);

    }

    public override WebResponse GetResponse()
    {
        MemoryStream responseCopy = new MemoryStream();
        //Stream responseCopy = new MemoryStream();
        responseMessage.Position = 0;
        responseMessage.CopyTo(responseCopy);
        //Reset position after reading Streams
        responseCopy.Position = 0;
        Mock<HttpWebResponse> mockHttpWebResponse = new Mock<HttpWebResponse>();
        mockHttpWebResponse.Setup(r => r.StatusCode).Returns(httpStatusCode);
        mockHttpWebResponse.Setup(r => r.GetResponseStream()).Returns(responseCopy);
        return mockHttpWebResponse.Object;
    }  

After this in my test I do this:

        public void DeviceChangedEvent_WhenDeviceHaveChanged_EventIsThrown()
    {
        string uri = new UriBuilder(TESTHOSTPREFFIX, TESTCORRECTHOST, TESTPORT, TESTDEVICEENDPOINT).ToString();
        bool wasThrown = false;
        m_deviceRetriever.Init(m_serviceProvider);
        m_deviceRetriever.Start();
        m_deviceRetriever.DeviceChangeEvent += (DeviceRetrieverOnDeviceChangeEvent, args) =>
        {
            wasThrown = true;
        };

        Thread.Sleep(5000);

        //Change device XML to simulate the change
        var namespaceManager = new XmlNamespaceManager(m_correctMockedXmlDevice.NameTable);
        namespaceManager.AddNamespace("ps", "http://ift.tt/1SbRASq");
        XmlNode printheadIdNode = m_correctMockedXmlDevices.SelectSingleNode("/ps:DevicesStatus/DeviceSlotCollection/DeviceSlot/SlotId", namespaceManager);
        deviceIdNode.InnerText = "Changed";
        m_testWebRequestCreateCorrectDevices = null;
        m_testWebRequestCreateCorrectDevices = new TestWebRequestCreate
        {
            testWebRequest = new TestWebRequest(HttpStatusCode.OK, m_correctMockedXmlDevices.InnerXml)
        };

        Thread.Sleep(5000);

        //We give some time to get the new state of printheads
        Assert.IsTrue(wasThrown);
    }
}

Before the test I'm preparing it creating this

    private void CreateCorrectDevicesMockEndpoint()
    {
        string uri = new UriBuilder(TESTHOSTPREFFIX, TESTCORRECTHOST, TESTPORT, TESTPRINTHEADSENDPOINT).ToString();
        m_testWebRequestCreateCorrectDevices = new TestWebRequestCreate();
        m_correctMockedXmlDevices = new XmlDocument();
        m_correctMockedXmlDevices.Load("pathToXMLFile");
        m_testWebRequestCreateCorrectDevices.testWebRequest = new TestWebRequest(HttpStatusCode.OK, m_correctMockedXmlPrintheads.InnerXml);
        WebRequest.RegisterPrefix(uri, m_testWebRequestCreateCorrectDevices);
    }

I'm not getting any error, the problem is that the XML returned by my mock is not changed. Thank you for your help!

Aucun commentaire:

Enregistrer un commentaire