我正在对 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???
最佳答案
回调
使用Setup
时,您可以使用Callback
方法。例如:
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);
关于c# - 调用方法时起订量验证属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43811395/