Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get NSubstitute to auto mock my concrete classes?

Tags:

nsubstitute

I have an interface which I am mocking with 'NSubstitute' which contains properties that return concreate classes, that is the return value is not an interface. e.g

public interface ISomething
{
    SomeObj First { get; }
    SomeObj Second { get; }
}

The 'SomeObj' concrete class has a default constructor but 'NSubstitute' always returns 'null' for these properties. The class itself is not under my control so I cannot simply make it derive from an interface.

Can 'NSubstitute' mock these type of properties? Or is there a way to override the behaviour? Otherwise I have to manually initialise the mock before the test and that can be a lot of code (even if its reused through a common method).

Perhaps there is a simpler solution that I have over-looked?

like image 769
karmasponge Avatar asked Jun 19 '13 03:06

karmasponge


People also ask

Can you mock concrete classes?

5 Answers. Show activity on this post. In theory there is absolutely no problem mocking a concrete class; we are testing against a logical interface (rather than a keyword interface ), and it does not matter whether that logical interface is provided by a class or interface . In practice .

Can Mockito mock concrete classes?

Mockito gives several overloaded versions of Mockito. Mocks method and allows creating mocks for dependencies. It is important to note that Mock can be created for both interface or a concrete class. When an object is mocked, unless stubbed all the methods return null by default.

What is NSubstitute?

NSubstitute is a friendly substitute for . NET mocking libraries. It has a simple, succinct syntax to help developers write clearer tests. NSubstitute is designed for Arrange-Act-Assert (AAA) testing and with Test Driven Development (TDD) in mind.

Which is the basic syntax for creating a substitute?

The basic syntax for creating a substitute is: var substitute = Substitute.


1 Answers

Classes will be auto-mocked if they have a default (parameterless) constructor and all its members are virtual (see the note in the intro of Auto and recursive mocks). The aim of this is to reduce the potential for unwanted (destructive?) side-effects if we are using a substitute and suddenly hit a non-virtual, unmocked code path that does bad stuff in an instance we thought was fake.

NSubstitute doesn't have a way override this behaviour. Instead, I'd recommend creating all your substitutes via your own factory method (e.g. a static Sub.For<T>(...) method in your test project) that uses NSubstitute to produce a substitute, then applies all the specific initialisation rules you need, like using reflection to stub out values for each class property.

Hope this helps.

Possibly related links:

  • I advise trying to avoid mocking types we don't own.
  • Stack Overflow: Is it recommended to mock concrete class?
  • Hacky factory method sample that subs properties using reflection.
like image 97
David Tchepak Avatar answered Oct 12 '22 05:10

David Tchepak