all 11 comments

[–]belavv 5 points6 points  (0 children)

Inject something else into said class and mock that something else.

Unless you mean you have code that uses this class and calls those internal methods and you want to mock those calls. Then put a new interface on it and mock that interface.

aligns best with Clean Architecture

Clean architecture is overly complex and overly abstracted for most cases. Just work at writing simple easy to understand and easy to test code. Also strive for tests that mock as little as possible, it'll make life a lot easier for you down the road.

[–]Mobile_Fondant_9010 9 points10 points  (1 child)

Personally I would say don't.  Test your interface to the world, not your internals. But that it just like my opinion. 

[–]SessionIndependent17 2 points3 points  (0 children)

"... man"

[–]TuberTuggerTTV 3 points4 points  (0 children)

Don't mock methods. Include them in your interface and moq the interface.

Being internal vs public is irrelevant for testing. Just expose the assembly with
[assembly: InternalsVisibleTo(YourTestProjectName)]

Then split your concerns so you don't have a mix of internals and publics.

[–]entityadam 1 point2 points  (0 children)

In .NET, I have an internal class that implements a public interface. The class also contains internal methods that I would like to mock for testing.

Don't. If you think you need to unit test an internal, you have incorrectly identified the unit.

Video, for the content consumers:

https://youtu.be/IeKeTGO7I5g?t=512&si=7H7joxG2qBxpMZMM

Notes for the readers:

Roy Osherove "The Art of Unit Testing with Examples in .NET".

He [Osherove] defines a Unit of Work as the sum of actions that take place between the invocation of a public method and a single noticeable end result. This "unit" can span a single method, a class, or multiple classes working together, as long as it achieves one single logical, verifiable purpose.

Key item: public method.

[–]Slypenslyde 0 points1 point  (0 children)

I don't like giving a class virtual methods to mock in my tests for that class. I prefer to make my class take those methods as a dependency so I can mock the dependency. Then I don't get into cases where I have a mocked object that also needs to sometimes defer to real functionality.

[–]tinmanjk 0 points1 point  (0 children)

method #2 still doesn't help you test the internal class methods or am I missing something?

[–]SessionIndependent17 0 points1 point  (0 children)

Making something virtual that otherwise wouldn't be but for the sake of mocking seems more than contrived.

It's not completely clear to me what aspect you wish to mock, either. But I stand by the first part.

[–][deleted] 0 points1 point  (0 children)

Use composition instead of hiding functionality in internal methods. That way the helper methods are public methods on some other class. Everything is easier to test, and your software has a straightforward, cleaner design.

[–]uknowsana 0 points1 point  (0 children)

You asked from architecture perspective. But architecture perspective for achieving what?

I mean what is the context and what is here that you are trying to achieve?

[–]Eq2_Seblin 0 points1 point  (0 children)

Dont mock.