Example #1
0
        public void MakeCreditDecision_Always_ReturnsExpectedResult(int creditScore, string expectedResult)
        {
            // We're using a concrete CreditDecision class instead of a mock.
            // If we look at CreditDecision.cs, each call to GetCreditDecision
            // has a built-in delay of 2.5 seconds before it returns a value.
            //
            // Since we have 5 test cases above, running this test will take a total
            // of 12.5 seconds to execute, as compared to the same test in
            // CreditDecisionTests, which executes in a matter of milliseconds.
            //
            // Although it may not seem like much now, as your test suite grows,
            // the time sink rapidly becomes painful.
            systemUnderTest = new CreditDecision(creditDecisionService);

            // Each time we execute this method, it will take 2.5 seconds before we
            // get a result
            var result = systemUnderTest.MakeCreditDecision(creditScore);

            // Fortunately assertion works as normal...
            Assert.That(result, Is.EqualTo(expectedResult));

            // Ah, but we can no longer 'prove' (using the test) that our
            // MakeCreditDecision method:
            //   - Actually called into CreditDecisionService
            //   - Passed the correct parameters to CreditDecisionService
            //   - Returned the actual value from CreditDecisionService without
            //   modifying it
            // In effect, all we know is that this method (hopefully)
            // returned the correct result, but we have no way to tell how
            // it actually arrived at that result.
            //
            // creditDecisionService.VerifyAll();
        }
        public void MakeCreditDecision_Always_ReturnsExpectedResult(int creditScore, string expectedResult)
        {
            mockCreditDecisionService = new Mock <ICreditDecisionService>(MockBehavior.Strict);
            mockCreditDecisionService.Setup(p => p.GetCreditDecision(creditScore)).Returns(expectedResult);

            systemUnderTest = new CreditDecision(mockCreditDecisionService.Object);
            var result = systemUnderTest.MakeCreditDecision(creditScore);

            Assert.That(result, Is.EqualTo(expectedResult));

            mockCreditDecisionService.VerifyAll();
        }
    public void MakeCreditDecision_Always_ReturnsExpectedResult(int creditScore, string expectedResult)
    {
        //Call to real service adds 2.5 secs per test. Adds 12.5 secs to test run
        systemUnderTest = new CreditDecision(creditDecisionService);

        var result = systemUnderTest.MakeCreditDecision(creditScore);

        // Fortunately assertion works as normal...
        Assert.That(result, Is.EqualTo(expectedResult));

        // Because we used "real" service rather than a mock can no
        // verify the correctness of theinteraction of MakeCreditDecision
        // method and CreditDecisionService
        // creditDecisionService.VerifyAll();
    }
    public void MockedMakeCreditDecision_Always_ReturnsExpectedResult(int creditScore, string expectedResult)
    {
        mockCreditDecisionService = new Mock <ICreditDecisionService>(MockBehavior.Strict);
        mockCreditDecisionService.Setup(p => p.GetDecision(creditScore)).Returns(expectedResult);

        systemUnderTest = new CreditDecision(mockCreditDecisionService.Object);
        var result = systemUnderTest.MakeCreditDecision(creditScore);

        Assert.That(result, Is.EqualTo(expectedResult));

        //Using Verify allow to verify correctness of dependencies i.e.
        //   - Actually called into CreditDecisionService
        //   - Passed the correct parameters to CreditDecisionService
        //   - Returned the actual value from CreditDecisionService without modifying it.
        //Can also user VerifyAll() but not as explicit;
        mockCreditDecisionService.Verify(m => m.GetDecision(It.IsAny <int>()), Times.Once);
    }
Example #5
0
        public void Make_credit_score_check(int creditScore, string creditScoreOutput, string expectedResult)
        {
            // "If your MakeCreditDecision method is invoked with this specific number (creditScore),
            //return this response (expectedResult). If it gets invoked with any other number, fail the test immediately". (that's part of MockBehavior.Strict)
            _mockCreditScoreService = new Mock <ICreditScoreService>(MockBehavior.Strict);
            _mockCreditScoreService.Setup(p => p.MakeCreditDecision(creditScore)).Returns(creditScoreOutput);

            _creditDecision = new CreditDecision(_mockCreditScoreService.Object);

            var result = _creditDecision.MakeCreditDecision(creditScore);

            Assert.That(result, Is.EqualTo(expectedResult));

            //Next up, we execute the MakeCreditDecision method just like we did before,
            //the only remaining step is to ask our Mock instance if all of its expectations were fulfilled using mockCreditDecisionService.VerifyAll()
            _mockCreditScoreService.VerifyAll();
        }
 public void CleanUp()
 {
     _mockCreditDecisionService = null;
     _systemUnderTest           = null;
 }
 public void Init()
 {
     _mockCreditDecisionService = new Mock <ICreditDecisionService>(MockBehavior.Strict);
     _systemUnderTest           = new CreditDecision(_mockCreditDecisionService.Object);
 }
 public void SetUp()
 {
     _systemUnderTest = new CreditDecision();
 }