Reputation: 3665
I am having trouble getting my Verify to work on a DB call.
I have a method that I am simply trying to verify that a database call was made.
I can't post the real code but here is a close example.
protected void ReportDB(uint waitTimeInMinutes)
{
//check database connection
Status dbStatus = Status.Ok;
string dbComment = "ok";
try
{
Data.GetActive("1");
}
catch (Exception ex)
{
dbComment = "Unable to access the database: " + ex.Message;
dbStatus = Status.Critical;
}
//Report Status.
}
So basically the GetActive() method just makes a database call. If it doesn't throw an exception then we are good and connectivity is up.
My test method is.
[TestMethod]
public void ReportDBStatusTest()
{
_fakeData.Setup(s => s.Data.GetActive(It.IsAny<string>()));
_unitUnderTest = new Service();
_unitUnderTest.ReportDB(0);
_fakeData.Verify(s => s.Data.GetActive(It.IsAny<string>()), Times.Once());
}
I debug through and the method is called and everything, yet the verify says it was called Times.Never. I think I may just be misunderstanding how to do this correctly.
Error:
Expected invocation on the mock once, but was 0 times: s => s.Data.GetActive(It.IsAny()) Configured setups and invocations:
Upvotes: 1
Views: 124
Reputation: 2101
The error is expected. This is because the 'Data' object inside the 'ReportDB' object is not the same as the 'Data' object inside the '_fakeData' object.
One workaround would be to externalize the 'Data' object in your 'ReportDB' object so that it can be mocked. Else, you need to alter your unit test.
Upvotes: 5