1283822
1283822

Reputation: 2012

Moq verify property when method is called

I am unit testing a wrapper around WebClient. I want to check at the time calling UploadString, the QueryString property is set to a specific value. I don't need to check the QueryString value after the whole method finish.

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???

Upvotes: 2

Views: 1113

Answers (1)

Seth Flowers
Seth Flowers

Reputation: 9190

Callback

You can use the Callback method when using Setup. For instance:

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);

Upvotes: 2

Related Questions