c# - Unit testing extension methods, had a go, is this right, or gone around the houses? -


i have poco library , have entities implement interface called ientitydelete.

interface simple, looks this

public interface ientitydelete  {     bool isdeleted { get; set; } } 

so have entity implements interface, again simple, looks this

public class myentity() : ientitydelete {     public bool isdeleted { get; set; } } 

i have extension method, created like

public static void markasdeleted(this ientitydelete entity) {     entity.isdeleted = true; } 

then needed check if method being called within 1 of service methods in unit tests. service method basic, looks this.

public task<int> deletebyflagasync(myentity entity) {     entity.markasdeleted();      return _context.savechangesasync(); } 

apparently cannot test extension methods easily, without using microsofts moles framework, not want dependency.

i did googl'ing , found 2 articles on this, , how it, , know if correct, or whether have done stupid.

two articles found where

http://adventuresdotnet.blogspot.co.uk/2011/03/mocking-static-methods-for-unit-testing.html http://blogs.clariusconsulting.net/kzu/how-to-mock-extension-methods/

they recommend using wrapper class aint static, ended this.

first created wrapper interface

public interface ientitydeletewrapper  {     void markasdeleted(ientitydelete entity); } 

create class implements interface

public class entitydeletewrapper : ientitydeletewrapper {     public void markasdeleted(ientitydelete entity)     {         entity.isdeleted = true;         entity.deleteddate = datetime.now;         entity.deletedbyuserid = 546372819;     } } 

inject interface service constructor

public myservice(ientitydeletewrapper deletewrapper) {     _deletewrapper = deletewrapper; } 

change service method call use wrapper so

public task<int> deletebyflagasync(myentity entity) {     _deletewrapper.markasdeleted(entity);      return _context.savechangesasync(); } 

solved told, way far go, can check if property has changed. in light of this, using extension method still , updated unit test this.

[testmethod] public void should_mark_entity_as_deleted() {     // arrange     var entity = new attachment     {         isdeleted = false     };      // act     var result = _service.deletebyflagasync(entity).result;      // assert     assert.areequal(true, entity.isdeleted);     _context.verify(e => e.savechangesasync(), times.once); } 

you went far. test should verify observable change state, not how change made. otherwise make tests brittle, not mention add rather unnecessary layer. enough check whether entity properties changed after deletebyflagasync call.

of course, when deleting gets more complex introducing dependency delegate task makes sense. then, few questions arise:

  • what scope of deletebyflagasync? call 2 dependencies?
  • would practical test it?
  • ...or perhaps tests said dependency suffice (as actual mark-for-deletion take place)?

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -