Dependency Injection and Unit test this constructor -


i have constructor , property in class:

private imycollectionobjects _mycollectionobjects;  public myclassconstructor(string message) {      _mycollectionobjects = mycollection.getcollectionobejects(message); } 
  1. with detail can please me understand how unit test constructor , getcollectionobjects method?

  2. how decouple classes? can give answer using ioc, want understand concept.

thank you.

for of all, unit testing unitary testing, is, 1 thing @ time.

with detail can please me understand how unit test constructor , getcollectionobjects method?

first things first, have unit tested mycollection class?

if not, should begin it, myclassconstructor class depends on it, basis of dependency injection. otherwise, how can manage know if results you're getting right or wrong? won't able test , sure works flawlessly.

how decouple classes? can give answer using ioc, want understand concept.

i humble point of view, must have clear reason make object dependant of using dependency injection. once make object depend on another, makes no sense, in opinion, decouple them. 1 way of decoupling might use unity application block of enterprise library.

unit test constructor

you need check 3 things while testing such constructor.

  1. that constructor doesn't return null value;
  2. that instance returns of expected type;
  3. that object expect instantiated through dependency instiated.
     [testcase("message")]     public void dependentconstructortest(string message) {         myclassconstructor myclass = new myclassconstructor(message);          assert.isnotnull(myclass);         assert.isinstanceof(typeof(myclassconstructor), myclass);         assert.isnotnull(myclass.mycollection); // mycollection represents property                                                  // exposes instance created of object                                                 // myclassconstructor class depends on.     } 

note: test written using nunit attributes , assertion methods. use whatever else like.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -