Esempio n. 1
0
        public void TestMockingSumService()
        {
            var value1    = 10;
            var value2    = 10;
            var operation = Operation.Sum;

            // Here the expected value is different from the sum between value1 and value2 just to show
            // Sum() will not be called actually. The mocked object will return a preprogramed value.
            var expectedValue = -1;

            _calculatorService = new CalculatorService(_mockedSumService, _subtractionService);

            // For any values received as parameter, Sum() will return the expectedValue.
            _mockedSumService.Sum(Arg.Any <int>(), Arg.Any <int>())
            .ReturnsForAnyArgs(expectedValue);

            var result = _calculatorService.ProcessOperation(value1, value2, operation);

            result.ShouldBe(expectedValue, $"Wrong answer! Result should be {expectedValue}, {result} was found.");

            // Asserts Sum() was called once.
            _mockedSumService.Received(1).Sum(Arg.Any <int>(), Arg.Any <int>());
        }