Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TDD: .NET following TDD principles, Mock / Not to Mock?

Tags:

c#

.net

tdd

mocking

I am trying to following TDD and I have come across a small issue. I wrote a Test to insert a new user into a database. The Insert new user is called on the MyService class, so I went ahead and created mytest. It failed and I started to implement my CreateUser method on my MyService Class.

The problem I am coming across is the MyService will call to a repository (another class) to do the database insertion.

So I figured I would use a mocking framework to mock out this Repository class, but is this the correct way to go?

This would mean I would have to change my test to actually create a mock for my User Repository. But is this recommended? I wrote my test initially and made it fail and now I realize I need a repository and need to mock it out, so I am having to change my test to cater for the mocked object. Smells a bit?

I would love some feedback here.

If this is the way to go then when would I create the actual User Repository? Would this need its own test?

Or should I just forget about mocking anything? But then this would be classed as an integration test rather than a unit test, as I would be testing the MyService and User Repository together as one unit.

I a little lost; I want to start out the correct way.

like image 267
Martin Avatar asked Feb 16 '23 03:02

Martin


1 Answers

So I figured I would use a mocking framework to mock out this Repository class, but is this the correct way to go?

Yes, this is a completely correct way to go, because you should test your classes in isolation. I.e. by mocking all dependencies. Otherwise you can't tell whether your class fails or some of its dependencies.

I wrote my test initially and made it fail and now I realize I need a repository and need to mock it out, so I am having to change my test to cater for the mocked object. Smells a bit?

Extracting classes, reorganizing methods, etc is a refactoring. And tests are here to help you with refactoring, to remove fear of change. It's completely normal to change your tests if implementation changes. I believe you didn't think that you could create perfect code from your first try and never change it again?

If this is the way to go then when would I create the actual User Repository? Would this need its own test?

You will create a real repository in your application. And you can write tests for this repository (i.e. check if it correctly calls the underlying data access provider, which should be mocked). But such tests usually are very time-consuming and brittle. So, it's better to write some acceptance tests, which exercise the whole application with real repositories.

Or should I just forget about mocking anything?

Just the opposite - you should use mocks to test classes in isolation. If mocking requires lots of work (data access, ui) then don't mock such resources and use real objects in integration or acceptance tests.

like image 99
Sergey Berezovskiy Avatar answered Mar 03 '23 10:03

Sergey Berezovskiy