Note that there are some explanatory texts on larger screens.

plurals
  1. POMoq in unit Testing
    primarykey
    data
    text
    <p>Let us say I have a Service </p> <pre><code>public interface IAreaService { int CalculateArea(int x,int y); int CalculateAreaTimesX(int x, int y, int ammount); } public class AreaService : IAreaService { public int CalculateArea(int x,int y) { return x*y; } public int CalculateAreaTimesX(int x, int y, int ammount) { return CalculateArea(x, y)*ammount; } } </code></pre> <p>With the relevant Unit tests</p> <pre><code>[TestMethod] public void AreaService_GetArea_Test() { AreaService service = new AreaService(); int expected = 9; int actual = service.CalculateArea(3, 3); Assert.AreEqual(expected,actual,"The Calculate area does not work as expected"); } [TestMethod] public void AreaService_GetAreaMultiplyByX_TestTrueValue() { AreaService service = new AreaService(); int expected = 27; int actual = service.CalculateAreaTimesX(3, 3, 3); Assert.AreEqual(expected,actual); } </code></pre> <p>Ok, so after running the Unit tests. I am sure that my method in question is working and life should be great.</p> <p>But now I want to use the IAreaService in another class, and this is where i lose the light. Here is the implementation of the other class.</p> <pre><code>public class PriceCalculatorService { private readonly IAreaService _areaService; public PriceCalculatorService(IAreaService areaService) { _areaService = areaService; } public double GetPrice(int x, int y, int times, double price) { return _areaService.CalculateAreaTimesX(x, y, times)*price; } } </code></pre> <p>If I ran the following unit test (My idea might be wrong on mocking here, and this is where the question comes in.</p> <pre><code>[TestMethod] public void PriceCalculatorService_GetPrice_Test() { var IAreaServiceMock = new Mock&lt;IAreaService&gt;(); IAreaServiceMock.Setup(ism =&gt; ism.CalculateAreaTimesX(2, 2, 2)).Returns(8); PriceCalculatorService priceCalc = new PriceCalculatorService(IAreaServiceMock.Object); double expected = 20; double actual = priceCalc.GetPrice(2, 2, 2, 2.50); Assert.AreEqual(expected,actual); } </code></pre> <h1>Question</h1> <p>When i run all the unit tests mentioned above then everything is good. All of them pass. But let us say i need for some reason to change the AreaService.Calculate() method to the following </p> <pre><code>public int CalculateArea(int x,int y) { return x*y+2; } </code></pre> <p>This means that my Unit test "AreaService_GetArea_Test()" will fail, like it should, but because of the Mocking used in the "PriceCalculatorService_GetPrice_Test()" test will still pass, because it seems that when you mock a Service then the actual code is not used(obviously). So my PriceCalculatorService_GetPrice_Test is useless. But I use a stub then the unit test will fail, because it should. </p> <p>So when to Mock, and when not to Mock ?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload