1

I've been tasked with changing someone else's integration tests into unit tests. We have business objects that talk to the database. And so our tests currently reflect that. The problem is that I have code that calls the db directly within the method - and I want it to hit mock data instead of the db. How do you do that?

List<listOfStuff> listing = getDataFromDB(DBStuff); //this is what I want to     
//not happen in my test.

I can't change the method, and I read something about wrapping the method in an interface, but I'm unsure of how do that...

NovaDev
  • 2,737
  • 5
  • 29
  • 43
  • Disclaimer I work at Typemock. You can do It with Typemock Isolator: `Isolate.WhenCalled( () => object.method(null) ).WillReturn(new List());` The call to the DataBase will be skipped and getDataFromDB will just return the list that you want. – Al.exe Apr 06 '15 at 07:33

1 Answers1

1

You can use MS Fakes

Fakes let you just change what

getDataFromDB(DBStuff); 

Does, but its better to write code that uses dependency injection and getDataFromDB is accessed via an interface.

This is a very simple Fake example.

  ShimDateTime.NowGet = () => new DateTime(2012, 12, 21);

We are making DateTime.Now return 21/12/2012 :) you can do the same with your getDataFromDB call.

Steve

Steve Drake
  • 1,968
  • 2
  • 19
  • 41