调用方法时起订量验证 属性
Moq verify property when method is called
我正在对 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);
我正在对 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);