0

I am trying to mock internal class for unit tests, due to some circular dependency I will not be able to use interface of this class to mock it, I also added unit test assembly name (made sure I am using correct assembly name from properties) to AssemblyInfo.cs of this project with InternalsVisibleTo , also added AssemblyInfo to csproj using <Compile Include="Properties\AssemblyInfo.cs" />, this makes class visible to test file but its not visible for mocking

NotSupportedException: Type to mock must be an interface, a delegate, or a non-sealed, non-static class.

I want to create mock of this class to do setup on other method in this class which I am not testing

2
  • stackoverflow.com/a/74406959/5045688 Commented Jan 4, 2024 at 3:41
  • 2
    There is always a way to go around such "circular dependencies" and in most cases it is the way to go to have testable code. Commented Jan 4, 2024 at 3:56

1 Answer 1

1

In order to be able to replace a concrete dependency with a Test Double, there must be some kind of polymorphism in place. There must be something to override or implement: abstract or virtual members, or an interface.

The exception message already tells you that:

Type to mock must be an interface, a delegate, or a non-sealed, non-static class.

It has nothing to do with whether the class in question is internal or public.

due to some circular dependency I will not be able to use interface of this class to mock it

Nothing prevents you from using interfaces, even in the presence of circular dependencies. See e.g. DIPPP for ways to deal with dependency cycles.

All that said, code will tend to be easier to maintain when it doesn't have circular dependencies, and when you only test against the public API.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.