Всем привет. В прошлой статье мы рассмотрели создание юнит-теста взаимодействия. Финальной частью курса будет рассмотрение настроенного поведения Moq.
Сам тест будет проверять утверждение полнолетнего заемщика с хорошей кредитной историей. Это тест-состояния, так как мы будем проверять возвращаемый объект.
Используя наш первый тест:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
[Test] public void ShouldDeclineUnderAgeApplicant() { // use Moq to create a fake version of an ICreditCheckerGateway var fakeGateway = new Mock<ICreditCheckerGateway>(); // create the sut and pass the fake version to it's constructor var sut = new CreditCardApplicationScorer(fakeGateway.Object); // create some test data var application = new CreditCardApplication { ApplicantAgeInYears = 20 }; // execute the behaviour var result = sut.ScoreApplication(application); // check expected result Assert.That(result, Is.False); } |
Первое, что наv нужно, – это установить приемлемый возраст для выдачи кредита (>20) и в проверке сделать изменение на Is.True.
Хорошая кредитная история задается с помощью:
1 |
fakeGateway.Setup(x => x.HasGoodCreditHistory(It.IsAny<string>())).Returns(true); |
Конечный вид теста:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
[Test] public void ShouldAcceptCorrectAgedApplicantWithGoodCreditHistory() { var fakeGateway = new Mock<ICreditCheckerGateway>(); fakeGateway.Setup(x => x.HasGoodCreditHistory(It.IsAny<string>())).Returns(true); var sut = new CreditCardApplicationScorer(fakeGateway.Object); var application = new CreditCardApplication { ApplicantAgeInYears = 30 }; var result = sut.ScoreApplication(application); Assert.That(result, Is.True); } |