Monday, October 24, 2016

Mocking Unit Tests: Get value out of a mocked method

Let's say you have a Save method, and you are unit testing it, and you want to get the "entity" that is getting passed in as a parameter to use in your test.

public voidSave(T entity)
         // Save my stuff

You can do this in your unit test:

FloppyDisk floppyDiskBeingSaved = null;

oldSkoolRepo.Setup(r => r.Save(It.IsAny())).Callback(en =>
{floppyDiskBeingSaved = en;}).Returns(() => Task.FromResult(floppyDiskBeingSaved));

Assert.AreEqual(stuff, floppyDiskBeingSaved);