نوشتن آزمونهای واحد به کمک کتابخانهی Moq - قسمت پنجم - نکات و مباحث تکمیلی
نویسنده: وحید نصیری
تاریخ: ۱۳۹۸/۰۷/۲۱ ۱۹:۲۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
var mockIdentityVerifier = new Mock<IIdentityVerifier>(MockBehavior.Strict);
Test method Loans.Tests.LoanApplicationProcessorShould.Accept threw exception: Moq.MockException: IIdentityVerifier.Initialize() invocation failed with mock behavior Strict. All invocations on the mock must have a corresponding setup.
mockIdentityVerifier.Setup(x => x.Initialize());
try
{
_creditScorer.CalculateScore(application.Applicant.Name, application.Applicant.Address);
}
catch
{
return application.IsAccepted;
}
mockCreditScorer.Setup(x =>
x.CalculateScore(It.IsAny<string>(), It.IsAny<string>()))
.Throws(new InvalidOperationException("Test Exception")); Assert.IsFalse(application.IsAccepted);
using System;
namespace Loans.Models
{
public class CreditScoreResultArgs : EventArgs
{
public int Score { get; set; }
}
} public interface ICreditScorer
{
event EventHandler<CreditScoreResultArgs> ResultAvailable; mockCreditScorer.Raise(x => x.ResultAvailable += null, new CreditScoreResultArgs());
mockCreditScorer.Setup(x =>
x.CalculateScore(It.IsAny<string>(), It.IsAny<string>()))
.Raises(x => x.ResultAvailable += null, new CreditScoreResultArgs()); namespace Loans.Tests
{
[TestClass]
public class LoanApplicationProcessorShould
{
[TestMethod]
public void AcceptUsingPartialMock()
{
var product = new LoanProduct {Id = 99, ProductName = "Loan", InterestRate = 5.25m};
var amount = new LoanAmount {CurrencyCode = "Rial", Principal = 2_000_000_0};
var applicant =
new Applicant {Id = 1, Name = "User 1", Age = 25, Address = "This place", Salary = 1_500_000_0};
var application = new LoanApplication {Id = 42, Product = product, Amount = amount, Applicant = applicant};
var mockIdentityVerifier = new Mock<IdentityVerifierServiceGateway>();
mockIdentityVerifier.Setup(x => x.CallService(applicant.Name, applicant.Age, applicant.Address))
.Returns(true);
var mockCreditScorer = new Mock<ICreditScorer>();
mockCreditScorer.Setup(x => x.ScoreResult.ScoreValue.Score).Returns(110_000);
var sut = new LoanApplicationProcessor(mockIdentityVerifier.Object, mockCreditScorer.Object);
sut.Process(application);
Assert.IsTrue(application.IsAccepted);
}
}
} public virtual bool CallService(string applicantName, int applicantAge, string applicantAddress)
public bool Validate(string applicantName, int applicantAge, string applicantAddress)
{
Connect();
var isValidIdentity = CallService(applicantName, applicantAge, applicantAddress);
LastCheckTime = DateTime.Now;
Disconnect();
return isValidIdentity;
} public bool Validate(string applicantName, int applicantAge, string applicantAddress)
{
Connect();
var isValidIdentity = CallService(applicantName, applicantAge, applicantAddress);
LastCheckTime = GetCurrentTime();
Disconnect();
return isValidIdentity;
}
public virtual DateTime GetCurrentTime()
{
return DateTime.Now;
} var expectedTime = new DateTime(2000, 1, 1);
mockIdentityVerifier.Setup(x => x.GetCurrentTime())
.Returns(expectedTime);
// ...
Assert.AreEqual(expectedTime, mockIdentityVerifier.Object.LastCheckTime); mockIdentityVerifier.Protected().Setup<bool>(
"CallService",applicant.Name, applicant.Age, applicant.Address)
.Returns(true);
var expectedTime = new DateTime(2000, 1, 1);
mockIdentityVerifier.Protected().Setup<DateTime>("GetCurrentTime")
.Returns(expectedTime); interface IIdentityVerifierServiceGatewayProtectedMembers
{
DateTime GetCurrentTime();
bool CallService(string applicantName, int applicantAge, string applicantAddress);
} mockIdentityVerifier.Protected()
.As<IIdentityVerifierServiceGatewayProtectedMembers>()
.Setup(x => x.CallService(It.IsAny<string>(),
It.IsAny<int>(),
It.IsAny<string>()))
.Returns(true);
var expectedTime = new DateTime(2000, 1, 1);
mockIdentityVerifier.Protected()
.As<IIdentityVerifierServiceGatewayProtectedMembers>()
.Setup(x => x.GetCurrentTime())
.Returns(expectedTime); using System;
namespace Loans.Services.Contracts
{
public interface INowProvider
{
DateTime GetNow();
}
} public class IdentityVerifierServiceGateway : IIdentityVerifier
{
private readonly INowProvider _nowProvider;
public DateTime LastCheckTime { get; private set; }
public IdentityVerifierServiceGateway(INowProvider nowProvider)
{
_nowProvider = nowProvider;
} public bool Validate(string applicantName, int applicantAge, string applicantAddress)
{
Connect();
var isValidIdentity = CallService(applicantName, applicantAge, applicantAddress);
LastCheckTime = _nowProvider.GetNow();
// ... var mockNowProvider = new Mock<INowProvider>(); mockNowProvider.Setup(x => x.GetNow()).Returns(expectedTime); var mockIdentityVerifier = new Mock<IdentityVerifierServiceGateway>(mockNowProvider.Object);