我有一个测试,从端点获取一些设备状态。我的反应被嘲弄了,得到了正确的信息。我想要实现的测试必须得到这个信息,过一段时间再得到这个信息,并检查抛出一个事件是否不同。问题是我不能在我的测试中更改被模仿的响应。这就是我试过的。
模拟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;
} 在我的测试中,我做了这个:
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://www.hp.com/ps/fall15");
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);
}
}在测试之前,我正在准备创建以下内容
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);
}编辑:我的测试设置是初始化所有必要的依赖项。
public void SetUp()
{
m_serviceProvider = new ServiceProvider();
m_serviceProvider.Add<ILogger>(new Mock<ILogger>().Object);
m_deviceRetriever = new DeviceRetriever();
m_connectivitySettings = new ConnectivitySettings() { IpAddress = TESTCORRECTHOST };
m_serviceProvider.Add<ConnectivitySettings>(m_connectivitySettings);
CreateCorrectDevicesMockEndpoint();
}我没有收到任何错误,问题是我的模拟返回的XML没有被更改。谢谢你的帮助!
发布于 2016-03-02 08:44:17
正如我在我的评论中所说的,我相信您的问题在于您没有向我们展示的代码。您正在设置的模拟很好,但是它依赖于您在正确的TestWebRequest实例上调用TestWebRequest。我相信,不管您是在轮询,您没有更新计票程序正在使用的TestWebRequestCreate实例,因此它只是创建了带有原始模拟的TestWebRequest设置的另一个实例。下面是一个示例,完整的测试,说明您的模拟工作,可以从一个不同的线程和工作的预期访问。
bool wasThrown = false;
IWebRequestCreate m_testWebRequestCreateCorrectDevices = null;
[TestMethod]
public void TestThreadedWebRequest()
{
const string errorResponse = "Some Error Text";
m_testWebRequestCreateCorrectDevices = new TestWebRequestCreate();
var uri = new Uri(@"http:\\www.stackoverflow.com");
wasThrown = false;
var thread = new System.Threading.Thread(() =>
{
while (true)
{
var creator = m_testWebRequestCreateCorrectDevices;
if (creator != null)
{
var message = creator.Create(uri);
var response = new StreamReader(message.GetResponse()
.GetResponseStream()).ReadToEnd();
if (response == errorResponse)
{
wasThrown = true;
break;
}
}
System.Threading.Thread.Sleep(500);
}
});
thread.Start();
System.Threading.Thread.Sleep(5000);
Assert.AreEqual(false, wasThrown);
m_testWebRequestCreateCorrectDevices = null;
m_testWebRequestCreateCorrectDevices = new TestWebRequestCreate
{
testWebRequest = new TestWebRequest(HttpStatusCode.OK, errorResponse)
};
System.Threading.Thread.Sleep(5000);
Assert.AreEqual(true, wasThrown);
if (!thread.Join(1000))
{
thread.Abort();
}
}注意,在现有代码中,将m_testWebRequestCreateCorrectDevices设置为创建正确模拟响应的新消息创建者的实例。这是不可能的,除非您的生产代码引用了这个变量(在我上面的例子中就是这样)。
https://stackoverflow.com/questions/35696913
复制相似问题