نوشتن آزمونهای واحد به کمک کتابخانهی Moq - قسمت اول - معرفی
نویسنده: وحید نصیری
تاریخ: ۱۳۹۸/۰۷/۱۷ ۱۴:۵۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
namespace Loans.Entities
{
public class Applicant
{
public int Id { set; get; }
public string Name { set; get; }
public int Age { set; get; }
public string Address { set; get; }
public decimal Salary { set; get; }
}
} namespace Loans.Entities
{
public class LoanProduct
{
public int Id { set; get; }
public string ProductName { set; get; }
public decimal InterestRate { set; get; }
}
} namespace Loans.Entities
{
public class LoanApplication
{
public int Id { set; get; }
public LoanProduct Product { set; get; }
public LoanAmount Amount { set; get; }
public Applicant Applicant { set; get; }
public bool IsAccepted { set; get; }
}
public class LoanAmount
{
public string CurrencyCode { get; set; }
public decimal Principal { get; set; }
}
} namespace Loans.Models
{
public class IdentityVerificationStatus
{
public bool Passed { get; set; }
}
} using Loans.Models;
namespace Loans.Services.Contracts
{
public interface IIdentityVerifier
{
void Initialize();
bool Validate(string applicantName, int applicantAge, string applicantAddress);
void Validate(string applicantName, int applicantAge, string applicantAddress, out bool isValid);
void Validate(string applicantName, int applicantAge, string applicantAddress,
ref IdentityVerificationStatus status);
}
} namespace Loans.Services.Contracts
{
public interface ICreditScorer
{
int Score { get; }
void CalculateScore(string applicantName, string applicantAddress);
}
} using System;
using Loans.Entities;
using Loans.Services.Contracts;
namespace Loans.Services
{
public class LoanApplicationProcessor
{
private const decimal MinimumSalary = 1_500_000_0;
private const int MinimumAge = 18;
private const int MinimumCreditScore = 100_000;
private readonly IIdentityVerifier _identityVerifier;
private readonly ICreditScorer _creditScorer;
public LoanApplicationProcessor(
IIdentityVerifier identityVerifier,
ICreditScorer creditScorer)
{
_identityVerifier = identityVerifier ?? throw new ArgumentNullException(nameof(identityVerifier));
_creditScorer = creditScorer ?? throw new ArgumentNullException(nameof(creditScorer));
}
public bool Process(LoanApplication application)
{
application.IsAccepted = false;
if (application.Applicant.Salary < MinimumSalary)
{
return application.IsAccepted;
}
if (application.Applicant.Age < MinimumAge)
{
return application.IsAccepted;
}
_identityVerifier.Initialize();
var isValidIdentity = _identityVerifier.Validate(
application.Applicant.Name, application.Applicant.Age, application.Applicant.Address);
if (!isValidIdentity)
{
return application.IsAccepted;
}
_creditScorer.CalculateScore(application.Applicant.Name, application.Applicant.Address);
if (_creditScorer.Score < MinimumCreditScore)
{
return application.IsAccepted;
}
application.IsAccepted = true;
return application.IsAccepted;
}
}
} using System;
using Loans.Models;
using Loans.Services.Contracts;
namespace Loans.Services
{
public class IdentityVerifierServiceGateway : IIdentityVerifier
{
public DateTime LastCheckTime { get; private set; }
public void Initialize()
{
// Initialize connection to external service
}
public bool Validate(string applicantName, int applicantAge, string applicantAddress)
{
Connect();
var isValidIdentity = CallService(applicantName, applicantAge, applicantAddress);
LastCheckTime = DateTime.Now;
Disconnect();
return isValidIdentity;
}
private void Connect()
{
// Open connection to external service
}
private bool CallService(string applicantName, int applicantAge, string applicantAddress)
{
// Make call to external service, interpret the response, and return result
return false; // Simulate result for demo purposes
}
private void Disconnect()
{
// Close connection to external service
}
public void Validate(string applicantName, int applicantAge, string applicantAddress, out bool isValid)
{
throw new NotImplementedException();
}
public void Validate(string applicantName, int applicantAge, string applicantAddress,
ref IdentityVerificationStatus status)
{
throw new NotImplementedException();
}
}
} public LoanApplicationProcessor(
IIdentityVerifier identityVerifier,
ICreditScorer creditScorer)
{
_identityVerifier = identityVerifier ?? throw new ArgumentNullException(nameof(identityVerifier));
_creditScorer = creditScorer ?? throw new ArgumentNullException(nameof(creditScorer));
} <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Loans\Loans.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.3.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.0.0" />
<PackageReference Include="MSTest.TestFramework" Version="2.0.0" />
</ItemGroup>
</Project> using Loans.Entities;
using Loans.Services;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Loans.Tests
{
[TestClass]
public class LoanApplicationProcessorShould
{
[TestMethod]
public void DeclineLowSalary()
{
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_100_000_0};
var application = new LoanApplication {Id = 42, Product = product, Amount = amount, Applicant = applicant};
var processor = new LoanApplicationProcessor(null, null);
processor.Process(application);
Assert.IsFalse(application.IsAccepted);
}
}
} public class LoanApplicationProcessor
{
private const decimal MinimumSalary = 1_500_000_0; Test method Loans.Tests.LoanApplicationProcessorShould.DeclineLowSalary threw exception: System.ArgumentNullException: Value cannot be null. Parameter name: identityVerifier
public LoanApplicationProcessor(
IIdentityVerifier identityVerifier,
ICreditScorer creditScorer)
{
_identityVerifier = identityVerifier ?? throw new ArgumentNullException(nameof(identityVerifier));
_creditScorer = creditScorer ?? throw new ArgumentNullException(nameof(creditScorer));
} > dotnet add package Moq > Install-Package Moq
using Loans.Entities;
using Loans.Services;
using Loans.Services.Contracts;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
namespace Loans.Tests
{
[TestClass]
public class LoanApplicationProcessorShould
{
[TestMethod]
public void DeclineLowSalary()
{
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_100_000_0};
var application = new LoanApplication {Id = 42, Product = product, Amount = amount, Applicant = applicant};
var mockIdentityVerifier = new Mock<IIdentityVerifier>();
var mockCreditScorer = new Mock<ICreditScorer>();
var processor = new LoanApplicationProcessor(mockIdentityVerifier.Object, mockCreditScorer.Object);
processor.Process(application);
Assert.IsFalse(application.IsAccepted);
}
}
}
using Loans.Entities;
using Loans.Services;
using Loans.Services.Contracts;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
namespace Loans.Tests
{
[TestClass]
public class LoanApplicationProcessorShould
{
[TestMethod]
public void Accept()
{
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<IIdentityVerifier>();
var mockCreditScorer = new Mock<ICreditScorer>();
var processor = new LoanApplicationProcessor(mockIdentityVerifier.Object, mockCreditScorer.Object);
processor.Process(application);
Assert.IsTrue(application.IsAccepted);
}
}
} _identityVerifier.Initialize();
var isValidIdentity = _identityVerifier.Validate(
application.Applicant.Name, application.Applicant.Age, application.Applicant.Address);