c# - How to Shim ExecuteReader with MS Fake -


i trying test method below error in line using (var reader = command.executereader()) connection must opened , available. not able shim method , return fake reader.

method test

 public emailtemplatefiltermodel[] getemailtemplatefilters()         {             var filterlist = new list<emailtemplatefiltermodel>();              using (var connection = new sqlconnection(_clientconnstring))             using (var command = connection.createcommand("cspgetinvoicetemplatefiltercollections"))             {                 if (connection.state != connectionstate.open)                     connection.open();                  using (var reader = command.executereader())                 {                     while (reader.read())                     {                         var filter = new emailtemplatefiltermodel                         {                             parmid = (int)reader["invoiceemailmapparmid"],                             parmname = (string)reader["parmname"],                             databasedatatype = (string)reader["dbdatatype"],                             controltype = (string)reader["controltype"],                             storedvalue = (string)reader["storedvalue"],                             displayedvalue = (string)reader["displayedvalue"]                         };                          filterlist.add(filter);                     }                 }             }             return filterlist.toarray();         } 

unit test

 [test()]         public void getemailtemplatefilterstest()         {             using (shimscontext.create())             {                 var rowcounter = 0;                  //arrange                 shimsqlconnection.allinstances.open = (sender) => { };                 shimsqlconnection.allinstances.stateget = (sender) => { return connectionstate.open; };                 //shimsqlcommand.allinstances.executereader = (sender) => new mock<sqldatareader>().object;                  shimsqldatareader.constructorsqlcommandcommandbehavior = (@this, p1, p2) =>                 {                     var shim = new shimsqldatareader(@this)                     {                          // valueget = () => -5                     };                 };                   shimsqldatareader.allinstances.read = (sender) =>                 {                     rowcounter++;                     return rowcounter <= 2;                 };                  shimsqldatareader.allinstances.itemgetstring = (sender, coloumnname) =>                 {                     if (rowcounter == 1)                     {                         switch (coloumnname)                         {                             case "invoiceemailmapparmid":                                 return 1;                             case "parmname":                                 return "param1";                             default:                                 return "somestrigvalue";                         }                     }                     else if (rowcounter == 2)                     {                         switch (coloumnname)                         {                             case "invoiceemailmapparmid":                                 return 1;                             case "parmname":                                 return "param1";                             default:                                 return "somestrigvalue";                         }                     }                     else                     {                         return string.empty;                     }                 };                  //act                 var filterlist = _sut.getemailtemplatefilters();                  //assert                 assert.that(filterlist.length, is.equalto(rowcounter));              }         } 

  [test()]         public void getemailtemplatefilterstest()         {             using (shimscontext.create())             {                 #region arrange                 var rowcounter = 0;                  shimsqlconnection.allinstances.open = (sender) => { };                 shimsqlconnection.allinstances.stateget = (sender) => connectionstate.open;                 shimsqlcommand.allinstances.executereader = (sender) => new shimsqldatareader();                 shimdbdatareader.allinstances.dispose = (sender) => { };                 shimdbdatareader.allinstances.disposeboolean = (sender, p1) => { };                  shimsqldatareader.allinstances.read = (sender) =>                 {                     rowcounter++;                     return rowcounter <= 2;                 };                  shimsqldatareader.allinstances.itemgetstring = (sender, coloumnname) =>                 {                     if (rowcounter == 1)                     {                         switch (coloumnname)                         {                             case "invoiceemailmapparmid":                                 return 1;                             case "parmname":                                 return "param1";                             default:                                 return "somestrigvalue";                         }                     }                     else if (rowcounter == 2)                     {                         switch (coloumnname)                         {                             case "invoiceemailmapparmid":                                 return 1;                             case "parmname":                                 return "param1";                             default:                                 return "somestrigvalue";                         }                     }                     else                     {                         return string.empty;                     }                 };                  #endregion                  //act                 var filterlist = _sut.getemailtemplatefilters();                  //assert                 assert.that(filterlist.length, is.equalto(2));              }         } 

Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -