use*_*438 3 c# unit-testing mocking
我想在下面的代码中测试FindPolicyToBeResent().我有几个选项,但是如果我的方法即最后一个选项没问题,我想知道其他人如何处理这种情况?
我的偏好是最后一个选项,但确实感觉有点脏,我在底部有一个例子
public class DocumentResendService : IDocumentResendService
{
#region Member Variables
...
#endregion
#region Constructors
...
#endregion
#region Accessors
...
#endregion
#region Methods
public IDocumentResendResponse ResendDocuments()
{
if (!IsInputsValid())
{
return response;
}
RecordRequestAttempt();
if (RequestLimitIsReached())
{
return response;
}
FindPolicyToBeResent();
if(PolicyCanNotBeResent())
{
return response;
}
RequestDocumentToBeResent();
return response;
}
private bool IsInputsValid()
{
..
}
private void RecordRequestAttempt()
{
...
}
private bool RequestLimitIsReached()
{
...
}
// I want to test this method which basically just filters the policies
private void FindPolicyToBeResent()
{
var allPolicies = policyDocumentRepository.GetPolicy(AgentCode, Email, PostCode, SearchDate, BirthDate);
policies = allPolicies.Where(currentPolicy => currentPolicy.IsActive() || currentPolicy.IsInTheFuture());
if (policies.Count() == 0 )
{
policies = allPolicies.FilterToPolicyThatHasEndedMostRecently();
}
}
private bool PolicyCanNotBeResent()
{
if (policies == null || !policies.Any())
{
response.Add(ErrorCode.PolicyCanNotBeFound);
return true;
}
foreach (var policy in policies)
{
// I could mock this line and use a verify here which policy id's are passed in
if (documentResenderRepository.CanPolicyBeResent(policy.Id) == false)
{
response.Add(ErrorCode.UnableToResendDocument);
}
}
return response.Errors.Any();
}
private void RequestDocumentToBeResent()
{
...
}
#endregion
}
Run Code Online (Sandbox Code Playgroud)
这是最后一个选项的单元测试解决方案
[TestFixture]
public class FindPolicyToBeResentTest : DocumentResenderTestsBase
{
private readonly List<Policy> allPolicies = new List<Policy>();
public FindPolicyToBeResentTest()
{
var day = -250;
for (int i = 1; i < 6; i++)
{
var policy = new Policy
{
Id = i,
StartDate = DateTime.Now.AddDays(day)
};
day = day + 100;
policy.EndDate = DateTime.Now.AddDays(day);
allPolicies.Add(policy);
}
}
private void SetUpDocumentResender(IEnumerable<Policy> policies)
{
SetUpObjectDefaultsForDocumentResenderCreation();
policyRepositoryMock.Setup(y => y.GetPolicy(It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<DateTime>(),
It.IsAny<DateTime>()))
.Returns(policies);
documentResendService = CreateDocumentResendService();
SetDocumentResenderDefaults();
}
[Test]
public void PoliciesThatAreNotActiveOrAreInThePastShouldBeFilteredOut()
{
SetUpDocumentResender(allPolicies);
documentResendService.ResendDocuments();
foreach (var policy in allPolicies)
{
if (policy.IsActive() || policy.IsInTheFuture())
{
documentResenderRepositoryMock.Verify(x => x.CanPolicyBeResent(policy.Id), Times.AtLeastOnce());
}
else
{
documentResenderRepositoryMock.Verify(x => x.CanPolicyBeResent(policy.Id), Times.Never());
}
}
}
[Test]
public void IfNoPoliciesAreFoundThatAreSuitableForDocumentResendingThenGetThePolicyThatHasMostRecentlyExpired()
{
var unsuitablePolicies = allPolicies.Where(x => x.IsActive() == false && x.IsInTheFuture() == false).OrderBy(x => x.EndDate);
var policyWithClosestToEndDateToNow = unsuitablePolicies.ToList().Last();
SetUpDocumentResender(unsuitablePolicies);
documentResendService.ResendDocuments();
documentResenderRepositoryMock.Verify(x => x.CanPolicyBeResent(policyWithClosestToEndDateToNow.Id), Times.AtLeastOnce());
foreach (var policy in allPolicies.Where(policy => policy != policyWithClosestToEndDateToNow))
{
documentResenderRepositoryMock.Verify(x => x.CanPolicyBeResent(policy.Id), Times.Never());
}
}
}
Run Code Online (Sandbox Code Playgroud)
通过公共方法测试私有方法很好.如果你的代码足够模块化,那么不应该有太多的设置代码,以便让条件正确进入你的私有方法.如果你发现自己设置了许多东西只是为了进入你的私人方法,你可能在一堂课中做得太多了.
在你的情况下,我很想接受你的观点3),并创建一个PolicyFinder:IPolicyFinder类.也许您现在不需要重复使用它,它可以使您的代码在将来更容易修改,并使两个类更容易测试
(见http://en.wikipedia.org/wiki/Single_responsibility_principle)
编辑:我没有完全阅读你的要点,很抱歉用单一的责任棒击中你;)
| 归档时间: |
|
| 查看次数: |
344 次 |
| 最近记录: |