vendredi 23 mars 2018

Moq: Verify function parameters being called have a specific value

I'm very new to Moq so please bare with me.

Basically I have a function

public Task CustomerUpdate(string user, JObject jobject)
{
   ...
   JObject newDocument = JObject.FromObject(jobject);
   newDocument["lastUpdate"] = Datetime.Utcnow();
   newDocument["someFlag"] = "foo";
   // change some more properties in newDocument

   // call the database provider
   await this.dbProvider.ReplaceAsync(user, newDocument);
}

In my unit test, I'm mocking like this:

dbProvider.Setup(p => p.ReplaceAsync(It.IsAny<string>(), It.IsAny<JObject>()))
                .Returns(Task.CompletedTask);

I need to verify that newDocument being passed as a parameter to dbProvider.ReplaceAsync method have the right property values.

Ideally I want to do this from my unit test:

Assert.IsEquals("foo", newDocument["someFlag"])

But my unit test does not have access to local variable inside CustomerUpdate function. And i think i cannot use callback either because the dbprovider isn't returning the updated object.

Any idea what can I do?

Aucun commentaire:

Enregistrer un commentaire