2. How to test Singleton class?
[TestMethod()] public void ReturnConnTest() { SClass target = new SClass(); string expected = "This is MOQ class"; string actual; Mock<SClass> moqClass = new Mock<SClass>(); moqClass.Setup(mc => mc.ReturnConn()).Returns(expected); target = moqClass.Object; actual = target.ReturnConn(); Assert.AreEqual(expected, actual); }3. Difference between Dependency Injection and Inversion of control.
Here is an informal definition of IoC: “IoC is when you have someone else create objects for you.” So instead of writing “new MyObject” in your code, the object is created by someone else. This ‘someone else’ is normally referred to as an IoC container.
This simple explanation illustrates some very important ideas:
- It is called IoC because control of the object is inverted. It is not the programmer, but someone else who controls the object.
- IoC is relative in the sense that it only applies to some objects of the application. So there may be IoC for some objects, whereas others are under the direct control of the programmer.