I am trying to unit test an AspNetCore.Mvc controller with Typemock. Here is the top layer of the controller code.
[HttpPost(UriFactory.FOO_ROUTE)]
public async Task<ActionResult> AddFooAsync([FromRoute]string novelid, [FromBody]AddFoo command)
{
var novel = await RetrieveNovel(novelid);
if (novel == null) return NotFound();
if (!ModelState.IsValid) return BadRequest(ModelState);
command.FooId = Guid.NewGuid();
novel.AddFoo(command);
await _store.SaveAsync(novel);
return Created(UriFactory.GetFooRoute(novel.Novel.NovelId, command.FooId), command);
}
Here is the test code I've written so far..
[TestClass, Isolated]
public class FooController_UnitTests
[TestMethod, Isolated]
public async Task TestMethod()
{
//Arrange
string novelid = "0";
Novel controllerUnderTest = Isolate.Fake.Dependencies<Novel>();
//Fake Foo
var ValidTestFoo = Isolate.Fake.Dependencies<AddFoo>();
//Fake the RetrieveNovel(novelid) call
Isolate.NonPublic.WhenCalled(controllerUnderTest, "RetrieveNovel").WillReturn(Task.FromResult(new GenreNovel()));
//Fake the ModelState
Isolate.WhenCalled(() => controllerUnderTest.ModelState.IsValid).WillReturn(true);
//Fake FooId
Isolate.NonPublic.Property.WhenGetCalled(ValidTestFoo, "FooId").WillReturn(Guid.NewGuid());
//Act
var fooResponse = controllerUnderTest.AddFooAsync(novelid, ValidTestFoo);
//Assert
//that RetrieveNovel(novelid) is called
Isolate.Verify.NonPublic.WasCalled(controllerUnderTest, "RetrieveNovel");
//that (!ModelState.IsValid) is called
Isolate.Verify.WasCalledWithAnyArguments(() => controllerUnderTest.ModelState.IsValid);
//that command.FooId was set with a Guid.NewGuid();
Isolate.Verify.NonPublic.Property.WasCalledSet(ValidTestFoo, "FooId").WithArgument(Guid.NewGuid());
}
}
FooId is an internal property of AddFoo. Right now I am trying to test that the FooId was set as a consequence of the AddFooAsync method logic, AND that it was set with a Guid.NewGuid.
My other 2 Verifies are passing but when I run the test with the last verify I get ....
TypeMock Verification: Property blahblahblah was called with mismatching arguments.
Thanks!