unit testing - Using Moq to verify in C# -
i'm trying mock database can verify save method called. have project saves database, takes list of objects save, , connection string.
this._database.save<constraint>(constraints, "default"); when debug can see test goes project mocked database, , hits save line once.
in test project create instance of class calling save method, create , create mock database, , use .setup save method.
private mock<idatabase> _mockdatabase; ... _mockdatabase = new mock<idatabase>(); _mockdatabase.setup(d => d.save<types.constraint>(it.isany<types.constraint>(), it.isany<string>())); then in test method, call .verify make sure save called 1 time.
_mockdatabase.verify(d => d.save<constraint>(it.isany<constraint>(), it.isany<string>()), times.once); however test fails on verify. know how can fix this? help/ideas!
moq.mockexception:
expected invocation on mock once, 0 times: d => d.save(it.isany(), it.isany())configured setups:
d => d.save(it.isany(), it.isany()), times.neverperformed invocations:
idatabase.save(system.collections.generic.list`1[types.constraint], "default")
with code, sending list<constraint> , expecting constraint so:
change setup :
_mockdatabase.setup(d => d.save<constraint>(it.isany<list<constraint>>(), it.isany<string>())); and verify :
_mockdatabase.verify(d => d.save<constraint>(it.isany<list<constraint>>(), it.isany<string>()), times.once);
Comments
Post a Comment