我正在测试围绕WebClient的包装器。我想在调用UploadString时检查,QueryString属性设置为特定值。整个方法完成后,我不需要检查QueryString值。
mockedWebClient.Setup(w=>w.UploadString("url2","POST","bodyyy")).Return("response");
mockedWebClient.Setup(w=>w.QueryString).Return(new NameValueCollection());
testibject.SomeMethod();
// Verify method was called
mockedWebClient.Verify(w=>w.UploadString("url2","POST","bodyyy");
// Also verify QueryString is set at the time UploadString is called???
答案 0 :(得分:1)
<强>回调强>
使用Callback
时,您可以使用Setup
方法。例如:
NameValueCollection queryString = new NameValueCollection();
mockedWebClient.Setup(w=>w.QueryString).Return(queryString);
bool isExpected = false;
mockedWebClient
.Setup(w=>w.UploadString("url2","POST","bodyyy"))
.Callback(() => isExpected = queryString["SomeKey"] == "SomeValue")
.Return("response");
testibject.SomeMethod();
Assert.IsTrue(isExpected);