chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
I using Iolsator 5.2 to mock a DB call as follows in MSTEST

[TestMethod]
 public void GetToken_ValidDomainUser_ReturnedToken()
 {
SqlConnection fakeConnection = Isolate.Fake.Instance<SqlConnection>(Members.ReturnRecursiveFakes);
            
Isolate.Swap.NextInstance<SqlConnection>().With(fakeConnection);
Isolate.WhenCalled(() => BlackMarble.Sabs.WebService.Utils.ConnectionString).WillReturn("mock_connection_string");

SqlCommand fakeCommand = Isolate.Fake.Instance<SqlCommand>(Members.ReturnRecursiveFakes);
SqlDataReader fakeReader = Isolate.Fake.Instance<SqlDataReader>(Members.ReturnRecursiveFakes);
Isolate.Swap.NextInstance<SqlCommand>().With(fakeCommand);
Isolate.WhenCalled(() => fakeCommand.ExecuteReader()).WillReturn(fakeReader);

Isolate.WhenCalled(() => fakeReader.Read()).WillReturn(true);

Isolate.WhenCalled(() => fakeReader.GetInt32(0)).WillReturn(0);
Isolate.WhenCalled(() => fakeReader.GetInt32(1)).WillReturn(1);
Isolate.WhenCalled(() => fakeReader.GetString(2)).WillReturn("Two");

// run the actual command            
var generator = new BlackMarble.Sabs.TokenGenerator.Generator();
var actual = generator.GetToken();

// check the results
Assert.IsNotNull(actual, "No token returned for user");
Assert.AreEqual(actual.UserID, 0);
Assert.AreEqual(actual.CompanyID, 1);
Assert.AreEqual(actual.DisplayName, "Two");
Assert.AreEqual(actual.IsValid(), true);
}



If it is run with testDriven.NEt it runs fine, but of run via TestDriven.Net and the deugger (i.e. VSHOST is involved) I get a

TypeMock Verification: Method System.Data.SqlClient.SqlConnection.Finalize() was expected but was not called

I tried

Isolate.Verify.NonPublic.WasCalled(fakeConnection ,"Finalize");

But this did not help, I assume I have mde a simple mistake but cannot spot it
asked by rfennell (6.8k points)

2 Answers

0 votes
Seems like a strange issue especially because you do not use Verify at the code at all...

Are you using the "Isolated" attribute? the error could result from previous test behavior not "cleaned" between tests.
answered by dhelper (11.9k points)
0 votes
That fixed it, I added the Isolated attribute and all seems OK.

Thanks for the pointer
answered by rfennell (6.8k points)
...