nsubstitute / NSubstitute

A friendly substitute for .NET mocking libraries.
https://nsubstitute.github.io
Other
2.69k stars 264 forks source link

Disabling Auto Values #233

Open mbordogna opened 8 years ago

mbordogna commented 8 years ago

It would be nice to have auto values disabled. I ran into a scenario for casting that was failing with the auto value, it took a bit for us to figure out this was due to a chain that was automatically created due to missing mocks because of the auto value and proxy in a service locator pattern due to differences in registration and the more generalized super interface.

If null was returned from the unmocked method, it would have been more obvious. There has been a few other times where this has been the case but when using nSubsitute to fill in the gaps with DI container testing for service locator patterns utilized for complex factories, having auto values made the issue less obvious.

So I am proposing an option for disabling auto values, either at construction or as a global setting. Like a ForExpicitMocks method, so that when used it will know to return null for anyting not mocked and does not attempt to create proxies.

dtchepak commented 8 years ago

I really like this idea, including being able to customise the autovalues used (say, all IEngine values should automatically get SimulatedEngine instances). I've never needed it enough to invest the time into getting it working though.

Is this something you've got time to look in to? I think the per-substitute option would be easiest to implement (I'm not sure how to handle the global config option, particularly if tests are running over multiple threads?).

dtchepak commented 8 years ago

Related: #234

mbordogna commented 8 years ago

I agree , method level is best bet. You know the code base better than me. How much time do you think would be involved? On Jun 8, 2016 6:57 PM, "David Tchepak" notifications@github.com wrote:

I really like this idea, including being able to customise the autovalues used (say, all IEngine values should automatically get SimulatedEngine instances). I've never needed it enough to invest the time into getting it working though.

Is this something you've got time to look in to? I think the per-substitute option would be easiest to implement (I'm not sure how to handle the global config option, particularly if tests are running over multiple threads?).

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/nsubstitute/NSubstitute/issues/233#issuecomment-224756286, or mute the thread https://github.com/notifications/unsubscribe/APlRIKigYxultfExCqf0ZUpSoWAqjg1jks5qJ0jwgaJpZM4Iw7_1 .

dtchepak commented 8 years ago

I'm not sure on how to do method level, but per-substitute should work. I'll see if I can get this to work with the implementation in #234 once I've merged it in.

stnoa commented 1 year ago

Was there any progress or guidance on this? We are currently migrating from MOQ and this is a bit of a headache for us.

dtchepak commented 1 year ago

Does the approach in https://github.com/nsubstitute/NSubstitute/pull/705#issuecomment-1285460860 work for this case?