c# - Trouble mocking return value of .Single() method in mocked repository -


scenario: learning how unit test. working on tests mvc action method nunit , fakeiteasy. have test verify method throws exception if passed id doesn't exist. action method calls repository wrapper method .single(), throw exception if nothing found. good.

in test, following:

  • create fake irepository using fakeiteasy
  • create test data
  • configure .single() wrapper method data test data

problem: having issues testing this. problem when passed invalid id, exception thrown right in configuration code fake repository, instead of in action method itself. the reason why obvious. configuration code ran before action method gets executed, , configuration code calls .single() on test data... (intentionally of course) not contain invalid id. throws exception right , there, , never makes action method. what not sure about, how around this. exception needs thrown inside action method. don't know how configure return value in way avoids conundrum.

code:
controller code

        public viewresult details(int id)         {             var dbpart = _repository                              .getsinglerecord<part>(x => x.partid == id);              var viewmodel = new detailsviewmodel()             {                 partid = dbpart.partid             };              return view(viewmodel);         } 


test code

        [testfixturesetup]         public void testfixturesetup()         {             // create fake partid exists             partid_that_exists = 1;              // create fake partid doesn't exist             partid_that_doesnt_exist = -100;         }          [test]         public void an_exception_is_thrown_if_the_part_doesnt_exist()         {             // arrange             fakerepository.fakepartid = partid_that_doesnt_exist;             _fakerepository = fakerepository.create();             _controller = new partcontroller(_fakerepository);              // act & assert             assert.throws<invalidoperationexception>(() =>                           _controller.details(partid_that_doesnt_exist));         } 


fake repository code

        public class fakerepository         {             public static int? fakepartid { get; set; }                            public static ibasicrepository create()             {                 // create fake repository                 var fakerepository = a.fake<ibasicrepository>();                  // create fake test data                 var fakeparts = new list<part>()                 {                     new part()                      {                          partid = 1, partdesc = "fake part 1"                     },                     new part()                      {                          partid = 2, partdesc = "fake part 2"                     }                 };                  // configure fake repository return fake data                 a.callto(() => fakerepository.getallrecords<part>())                                              .returns(fakeparts);                 if (fakepartid.hasvalue)                 {                     /* below code problem */                     a.callto(fakerepository)                    .where(call => call.method.name == "getsinglerecord")                    .withreturntype<part>()                    .returns(fakeparts.single(x => x.partid == fakepartid));                 }                  // return newly created & configured fakerepository                 return fakerepository;             }         } 

i figured out. needed use returnslazily() instead of returns().

returnslazily delays setting method's return values until method called, instead of setting them when method's configuration code executed.

new, working code:

a.callto(fakerepository)  .where(call => call.method.name == "getsinglerecord")  .withreturntype<part>()  .returnslazily(() => fakeparts                        .single(x => x.partid == fakepartid)); 

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -