我想在下面的代码中测试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
}
以下是最后一个选项的单元测试解决方案,但
[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());
}
}
}
答案 0 :(得分:5)
通过公共方法测试私有方法很好。如果你的代码足够模块化,那么不应该有太多的设置代码,以便让条件正确进入你的私有方法。如果你发现自己设置了许多东西只是为了进入你的私人方法,你可能在一堂课中做得太多了。
在你的情况下,我很想接受你的观点3),并创建一个PolicyFinder:IPolicyFinder类。也许您现在不需要重复使用它,它可以使您的代码在将来更容易修改并使两个类更容易测试
(见http://en.wikipedia.org/wiki/Single_responsibility_principle)
编辑:我没有完全阅读你的要点,很抱歉用单一责任棒击中你;)
答案 1 :(得分:0)
您可以将方法标记为内部方法而不是私有方法,然后使用AssemblyInfo.cs文件中的InternalsVisibleTo属性来允许单元测试程序集访问这些内部方法。例如,汇编OVReadySDK的AssemblyInfo文件中的此语句:
[assembly: InternalsVisibleTo("OVReadySDKUT, PublicKey=002400000480...5baacad")]
允许单元测试程序集OVReadySDKUT中的测试方法访问OVReadySDK中的类和方法,就好像测试方法是相同的程序集一样。
您可以通过搜索“InternalsVisibleTo单元测试”找到相关技术的一些示例。请注意,如果您的程序集已签名,则需要在InternalsVisibleTo
语句中提供公钥参数。