asp.net - NSubstitute HttpPostedFileBase SaveAs -


on journey of unit testing code , have code:

        var ufile = substitute.for<httppostedfilebase>();         var server = substitute.for<httpserverutilitybase();         var saved = false;         ufile.filename.returns("somefilename");          var filename = fs.path.getfilename(ufile.filename);         var path = fs.path.combine(server.mappath(upath), filename);         ufile.when(h => h.saveas(path)).do(x => saved = true);         assert.istrue(saved); 

so here testing gleaned different sites:

public actionresult uploadfiles()     {         var fileinfo = new list<uploadedimageviewmodel>();          foreach (string files in request.files)         {             var hpf = request.files[files] httppostedfilebase; //              if (hpf != null && hpf.contentlength > 0)                 continue;             var filename = path.getfilename(hpf.filename); //gets name of file uploaded             var temppath = path.combine(server.mappath("~/uploadtemp/"), filename); // creates string representation of file location             hpf.saveas(temppath);             //resize image before save right folder under fileuploads          }         return view(fileinfo);     } 

can please me understand when().do() syntax of nsubstitute? in docs, says should have action in need examples understand.

then saveas() method of httppostedfilebase void , in nsubstitute docs, says use when().do() void methods please tell me wrong unit test.

//suppose have setup public class myclass {     string returnsomething()      {         return "foobar";     }      void dosomething(out string reason){         reason = 'oops';     } } 

the usual stubbing syntax nsubstitute use returns this:

myclass.returnsomething().returns("wibble"); 

this stubs out returnsomething(), returns syntax works methods return value.

for methods have no return can instead use when().do(). meant action in documentation (as opposed func, have return value). common need fill in output parameters on such methods:

string reason; myclass.when(c => c.dosomething(out reason))     .do(args =>      {         args[0] = "fail";     }); 

for more on action , func see msdn: action, func.

in specific case of unit test, instead of setting variable saved when saveas invoked, consider asserting using nsubstitute.received construct instead.


Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -