I have a constructor and a property in the class:
private IMyCollectionObjects _myCollectionObjects;
public MyClassConstructor(string message)
{
_myCollectionObjects = MyCollection.GetCollectionObejects(message);
}
-
With as much detail can you please help me understand how to unit test this constructor
and GetCollectionObjects method? -
How do I completely decouple the
classes? You can give the answer
using any IoC, I want to
understand the concept.
Thank you.
For of all, unit testing is all about unitary testing, that is, one thing at a time.
First things first, have you unit tested your
MyCollectionclass?If not, you should begin with it, as your
MyClassConstructorclass depends on it, that is the basis of dependency injection. Otherwise, how can you manage to know if the results you’re getting are right or wrong? You won’t be able to test and be sure that it works flawlessly.I my humble point of view, you must have a clear reason to make an object dependant of another using dependency injection. Once you make an object depend on another, it makes no sense, in my opinion, to decouple them. One way of decoupling might be to use Unity Application Block of Enterprise Library.
You generally only need to check for three things while testing such a constructor.
[TestCase("message")] public void DependentConstructorTest(string message) { MyClassConstructor myclass = new MyClassConstructor(message); Assert.IsNotNull(myclass); Assert.IsInstanceOf(typeof(MyClassConstructor), myclass); Assert.IsNotNull(myclass.MyCollection); // Where MyCollection represents the property that // exposes the instance created of the object from // which your MyClassConstructor class depends on. }Note: This test is written using NUnit attributes and assertion methods. Use whatever else you like.