-1

I have a basic understanding of Mocking and fake objects, and passing the fake data for Unit test case methods. But is it possible that we can use actual data return by our repositories instead of creating our own fake data to our Unit test methods.

I am using NUnit for a .Net MVC 5 application. As shown below in sample code line :

mockType.Setup(x=>x.GetUserDetails().Returns(
    new UserDetail(){ id = 1 , name = "John"}
);

So I need to return the actual data return from method GetUserDetails, instead to create the fake data (as we did in above example).

I need to get the user details from DB table instead of creating fake UserDetail as we did in above example. Please give your advice and let me know if you need more information.

Thanks in advance.

mayank gupta
  • 341
  • 1
  • 4
  • 16
  • 1
    You don't need to mock GetUserDetails() if you want it to return data from your database, why don't you just setup a test database just like you would do normally? – dbraillon Oct 24 '17 at 12:07
  • You have give an good idea, but everytime there are changes in our original database we need to do the same changes in our test database. Just to let you know, we can play with our actual database without any problem on all the environment except production. – mayank gupta Oct 24 '17 at 12:12
  • Also, I would like to know the way to get the data from our repository methods (which are already written) insted of creating fake data objects. Let me know if i am not make sense. – mayank gupta Oct 24 '17 at 12:14
  • Well, if it's already written then use it, the way you use it normally, just change the connection string for your test project. – dbraillon Oct 24 '17 at 12:19
  • Then if you want to run your test with data from updated data, just copy it before you run your test. – dbraillon Oct 24 '17 at 12:20
  • In Unit test Project class, can we access services and services in a same way, like we did it in our MVC controllers ? – mayank gupta Oct 24 '17 at 12:30
  • Oh, I see your point. You should have a separate library containing all your logic, both database and business layers. Then reference it in your MVC app AND your test project. – dbraillon Oct 24 '17 at 13:47
  • Exactly this is my question, thanks to understand. So can we use service methods directly in my test method ? Without mocking ? – mayank gupta Oct 24 '17 at 13:52
  • Sure ! Mocking is about faking, I your case you don't want that, you want to use real code logic to test it with real production data. Extract all your logic from your MVC project into a library project so both MVC and test projects can reference it – dbraillon Oct 24 '17 at 14:01
  • Thanks This is what i need to confirm .. – mayank gupta Oct 24 '17 at 14:06

1 Answers1

0

Tests which access other layers of your application called "Integration tests" or "Acceptance tests" if you testing full "pipeline" of your application UI -> Database -> UI.

In your particular case you need to have empty database with exactly same schema as used in the production.

  • Insert data for test case
  • Execute test
  • Assert results
  • Clean database

Accessing real database, insert data, clean database after every tests will make tests slower - to be slow is OK for integration or acceptance tests.

For keeping testing and production database schema in sync, you should have some maintaining scripts/logistics of your choice.

Fabio
  • 31,528
  • 4
  • 33
  • 72
  • That was a good explanation. IF I go again with the same mocking setup, is it possible to provide the actual data to my mock repository ? As shown in original question. – mayank gupta Oct 24 '17 at 12:33
  • Yes it is possible - just call actual implementation of `GetUserDetails`. Possible you need configure actual services with correspondent connections string and other dependencies. – Fabio Oct 24 '17 at 12:38
  • It would be helpful if you can show me some example, if you have any, Thanks! – mayank gupta Oct 24 '17 at 12:43
  • See how configuration made in your production project - use same approach. – Fabio Oct 24 '17 at 12:44