c# - How to moq setup IRepository<T> -


i trying setup this:

[test] public void observatoins_were_returned() {     using (var mock = automock.getloose())     {         // arrange         mock.mock<irepository<wspobservation>>()             .setup(x => x.getall())             .returns(_observations);          var sut = mock.create<commonservices>();                                       wspresponse wspresponse;          // act         var wspobservations = sut.getallobservations(out wspresponse);         var expectederrorcode = responsecodes.success;          // assert         // assert.areequal(expectederrorcode, wspresponse.responsecode);      } } 

but when getallobservations() function called returns null in actual code.

in actual code irepository dependency injected working fine.

object being returned looks this.

      var _observations = new list<wspobservation>();         _observations.add(new wspobservation() { devicename = "devcie one", steps = "3000"  });         _observations.add(new wspobservation() { devicename = "devcie one", steps = "2000" }); 

the actual function being tested looks this

public list<wspobservation> getallobservations(out wspresponse getallwspobservationsresponse)     {         list<wspobservation> allwspobservations = new list<wspobservation>();         getallwspobservationsresponse = new wspresponse();         try         {             //some other business logic             allwspobservations = _wspobsrep.getall().tolist();             //some other business logic         }         catch (exception ex)         {             getallwspobservationsresponse.responsecode = responsecodes.databasegeterror;          }         return allwspobservations;     } 

dependency injection looks this

    private irepository<wspobservation> _wspobsrep;      public commonservices(irepository<wspobservation> wspobsrep)     {         _wspobsrep = wspobsrep;     } 

what intention of

var sut = mock.create<commonservices>(); 

isn't better create real sut object , inject irepository mock?

i way:

var repomock = mock.mock<irepository<wspobservation>>()         .setup(x => x.getall())         .returns(_observations);  var sut = new commonservices(repomock); 

Comments