How to write an internal mock method

We use Moq as our mocking structure, the problem is that the type that needs to be mocked is executed using the interface, the problem with this is something in this interface, will be publicly available and, therefore, is considered part of our public API.

is there a way to have a member that is mock and not public?

+4
source share
2 answers

If you correctly understood that you want to apply the interface to the type in order to support mockery, make it so that the interface is not visible to public users of your code.

Well, one option is that you can implement the internal interface and use [assembly:InternalsVisibleToAttribute] to make internal types available for your unit tests.

 [assembly:InternalsVisibleTo("MyUnitTestAssembly")] internal interface ISomeInterfaceForMocking { ... } public class MyMockableType : ISomeInterfaceForMocking { ... } 
+3
source

Is http://code.google.com/p/moq/wiki/QuickStart#miscellaneous (see the "Miscellaneous" section) for any use (i.e. the syntax for protected bullying)?

ISP says there shouldn’t be any magical things on the interface that only a few people are interested in. In addition, did you consider that you adhere to one virtual method, and not the whole interface and / or the basic interface for the mocked bit? Or just have a specific interface for this. Remember that if it is difficult to mock or test, in general, something can be improved in your code (as opposed to finding techniques for jumping hoops and / or overflowing overly powerful mocking frameworks).

In addition, I bet that if you publish an abridged version of your test, someone will be able to reorganize it just, and we all will know something.

0
source

Source: https://habr.com/ru/post/1313803/


All Articles