c# - MOQ object setup scope / context being lost with builder pattern unit test and web api controller -


i writing unit test controllers , running issue seems mocked object not getting setup when inject it. if create mocked object in controller end point works fine. further complicate things doing unit testing via fluent builder pattern.

first here working snip:

public httpresponsemessage addslot(addslotrequest requestlist) {    var errorinfo = new list<errorinfo>();     mock<idatrepository> _xrepository = new mock<idatrepository>();     _xrepository.setup(x => x.getdat(it.isany<guid>(), ref errorinfo))      .returns(new dat());   ....snip....   var crt = _xrepository.object.getdat(datid, ref errorinfo);   //crt object returned expected mock   ....snip  } 

now heads gets little more involved.

first have controller being created builder:

public class addslotcontrollerbuilder {     //private backing field fluent methods act upon     private mock<idatrepository> _xrepository = new mock<idatrepository>();      //exposed property external interactions...     //not doing yet had plans why here     public mock<idatrepository> moqdatrepository { get; private set; }      //following builder pattern build method called return      //the object after configuration complete     public addslotcontroller build()     {         var errorinfo = new list<errorinfo>();        _xrepository.setup(x => x.getdat(it.isany<guid>(), ref errorinfo)).returns(new dat());           //assign private mock field property         moqdatrepository = _xrepository;          return new addslotcontroller(_xrepository.object);     } } 

now unit test uses builder:

[testmethod] public void addvalidpickupslotcorrectresponse() {    var errorinfo = new list<errorinfo>(); //ref field      //arrange    //1. controller    addslotcontroller controller = new addslotcontrollerbuilder().build();    //skipping apsr brevity     //act    controller.addpickupslot(apsr);  } 

finally in controller

  var crt = _xrepository.object.getdat(datid, ref errorinfo);   //crt null???? 

any pointers i'm getting off track appreciated.

this issue being caused getdat method taking ref parameter. essentially, unless you're passing same instance call you're passing setup, fail match setup. more details can found here, along suggested workaround using rhinomocks. think it'd work nsubstitute, haven't tested that.

looking @ you're doing though, really need errorinfo passed ref? it's instance of list class, presumably repository might add to. going need reassign errorinfo different instance of class? if not, change signature non-ref 1 , set mock follows (either should work):

_xrepository.setup(x => x.getdat(it.isany<guid>(), it.isany<list<errorinfo>>()))                          .returns(new dat()); 

or

_xrepository.setup(x => x.getdat(it.isany<guid>(), errorinfo))                          .returns(new dat()); 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -