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
Hi,

Consider the following section of a unit test;

SqlCeCommand command = Isolate.Fake.Instance<SqlCeCommand>(Members.ReturnRecursiveFakes);
Isolate.Swap.AllInstances<SqlCeCommand>().With(command);
SqlCeDataReader reader = Isolate.Fake.Instance<SqlCeDataReader>(Members.ReturnRecursiveFakes);
Isolate.WhenCalled(() => reader.Read()).WillReturn(true);
Isolate.WhenCalled(() => reader.Read()).WillReturn(false);
Isolate.WhenCalled(() => command.ExecuteReader()).WillReturn(reader);

The section of code under test is essentially the following;

using (SqlCeCommand command = new SqlCeCommand("SQL STATEMENT", _connection))
{
using (SqlCeDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
...
}
}

_connection.Close();
}

The unit test runs through fine, at the end however it appears to be trying to finalize the SqlCeDataReader and causing an exception. I've tried calling GC.SupressFinalize(reader) both before and after the method under test is executed but the same exception is raised.

I guess that the following line of code from the unit test is actually not changing the reference of the underlying data reader;

Isolate.WhenCalled(() => command.ExecuteReader()).WillReturn(reader);

And so the SupressFinalize is only relative to the data reader created within the context of the unit test.

Is there anyway of suppressing the finalizer for the data reader within the method under test?

Thanks
Steve.
asked by stephencampling (3k points)

8 Answers

0 votes
Hi

Try faking the SqlCeCommand.Dispose method

SqlCeCommand command = Isolate.Fake.Instance<SqlCeCommand>(Members.ReturnRecursiveFakes); 
Isolate.WhenCalled(() => command.Dispose()).IgnoreCall();
// ... rest of the test


Please let me know if it helps.
answered by ohad (35.4k points)
0 votes
Hi,

Thanks for the response. I've already tried this and it doesn't fix the issue. I assume this is because i'm faking the call to Dispose() rather than the overloaded call to Dispose(bool disposing) that the finalizer calls?

Regards
Steve
answered by stephencampling (3k points)
0 votes
Hi,

Can you please post the exception message with the stack trace?
It seems to behave differently on my machine :(
answered by ohad (35.4k points)
0 votes
The exception message is;

"Value cannot be null."

And the stack trace is;

at System.Threading.Monitor.Enter(Object obj)
at System.Data.SqlServerCe.SqlCeDataReader.Dispose(Boolean disposing)
at System.Data.SqlServerCe.SqlCeDataReader.Finalize()

Many thanks
Steve.
answered by stephencampling (3k points)
0 votes
Hi,

It seems like you need to swap the next instance of the the reader object otherwise there is no point in setting the behavior of the reader object.
Try the following:

SqlCeDataReader reader = Isolate.Fake.Instance<SqlCeDataReader>();
Isolate.Swap.NextInstance<SqlCeDataReader>().With(reader);


That been said since I didn't reproduce the exception I suggest that if that doesn't work try sending a small repro to our support mail:
support at typemock.com
We'll try to help you to solve the problem.
answered by ohad (35.4k points)
0 votes
Hi - the line of code from the original code example that is responsible for returning the mocked data reader is as follows;

Isolate.WhenCalled(() => command.ExecuteReader()).WillReturn(reader);

I have managed to create a small reproduction, I'll mail that over to the address supplied.

Many thanks for looking into this.
Steve.
answered by stephencampling (3k points)
0 votes
Hi - Any update on this?

Many thanks
Steve
answered by stephencampling (3k points)
0 votes
Hi Stephen,

Sorry, not yet, we are still working on this. I will keep you updated as soon as I have some (hopefully good!) news :)
answered by igal (5.7k points)
...