Is it good practice to use NSubstitute(or other testing frameworks that allow mocking) in the code(not in the tests)?

Skip to first unread message

anton tishchenko

Feb 14, 2019, 11:51:44 AM2/14/19
to NSubstitute

I faced with usage of NSubstitute inside business logic(outside of test classed):

var extension = Substitute.For<IExtension>();

I get used to utilization of NSubstitute inside test classes, when you need to mock some class(interface). But using NSubstitute outside of test classes confused me. Is it correct place for it? Is is correct to use NSubstitute like dependency injection container, that can create of instance of interface/class?

My concerns are that NSubstitute was designed to be used for tests. Performance inside tests is not very important thing, that is why it could be slow. Also, it relies on reflection, so it could not be very quick. But, is performance of NSubstitute poor, or is it ok?

Are there any other reasons, why NSubstitute or other mocking libraries should not be used outside of tests?

David Tchepak

Feb 14, 2019, 5:42:53 PM2/14/19
Hi Anton,

I have previously written about this here:
I still stand by this answer! :)

Let me know if you need any additional information.

You received this message because you are subscribed to the Google Groups "NSubstitute" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To post to this group, send email to
Visit this group at
For more options, visit
Reply all
Reply to author
0 new messages