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
0 votes
I am setting up a fake like this:

IDataReader idr = new StubDataReader(resultSet);

SafeDataReader fakeSDR = Isolate.Fake.Instance<SafeDataReader>(Members.CallOriginal, ConstructorWillBe.Called, idr);


Hoping to cause this code that is being tested:

_sdr = new SafeDataReader(Command.ExecuteReader());

To set _sdr = fakeSDR. Is this possible?

I can make it work by wrapping the _sdr statement in a method and using

Isolate.WhenCalled(() => DAL.ExecuteReader()).WillReturn(fakeSDR);

but I'm trying to understand why the Swap.NextInstance doesn't seem to work. _sdr is always null.

Also, not sure if it's or TypeMock but my breakpoints are being ignored when I run a test. The only way I can debug is by calling System.Diagnostics.Debugger.Break(); Have a feeling I'm missing something stupid but I'm asking anyway.


Dave Erwin
asked by dave.erwin (1.3k points)

1 Answer

0 votes
Hi Dave,

Swap.NextInstance does not really swap objects. It swaps behaviors. What that means that when a method will be called on the real object in your production code, the method would actually run on the fake one. The difference of how methods will be have is how you built your fake object.

In your case, you create the fakeSDR to be really created with the IDR, but _sdr is created with what DAL.ExecuteReader returns. All methods later will behave as you expect, but the constructor doesn't. It also explains why using WhenCalled return the data reader works- you've initialized the real object as you did the fake one, which succeeds.

We have in our backlog a feature that will do "real" swapping, i.e. reference swapping. We'll let you know when we get there.

As to your breakpoints: this is strange. Area ll breakpoints missed? If you put them inside faked method, they will not be reached, so please make sure you are putting them inside calls that actually run.
Try to disable Isolator (Tools->Disable Isolator) and run the tests again, see if your breakpoints are met.

answered by gilz (14.5k points)