테스트중인 레거시 프로젝트를 가져 오려고합니다. 코드는 일반적으로 테스트 할 수있는 방식으로 작성되었지만 일부 타사 종속성은 그렇지 않습니다. 나는이 모양이 어떻게 단위 테스트, 뭔가 주위에 내 머리를 정리하려고 해요 :단위 테스트 작업 구성 요소 C#
class InsightEmailJob : NHibernateJob
{
public IInsightEmailService InsightEmailService { get; set; }
public IReportService ReportService { get; set; }
public ITemplatedNotifier TemplatedNotifier { get; set; }
public string ReplyEmail { get; set; }
public string ReplyName { get; set; }
public InsightEmailJob(ISession session,
ILog log,
IInsightEmailService insightEmailService,
IReportService reportService,
ITemplatedNotifier templatedNotifier,
SystemReplyEmailSpec systemReplyEmailSpec)
: base(session, log)
{
InsightEmailService = insightEmailService;
ReportService = reportService;
TemplatedNotifier = templatedNotifier;
ReplyEmail = systemReplyEmailSpec.ReplyEmail;
ReplyName = systemReplyEmailSpec.ReplyName;
}
public int AccountID{ get; set; }
private Account mAccount;
public Account Account
{
get
{
if (this.mAccount == null)
{
mAccount = this.InsightEmailService.Get<Account>(AccountID);
}
return mAccount;
}
}
protected override void DoWork(JobExecutionContext context)
{
var insightEmail = InsightEmailService.FindAndIncrementEmailForAccount(Account);
var report = ReportService.LoadMultiReportByName(insightEmail.ReportName);
var reportData = ReportService.Execute(report, new ParameterValuesDictionary(Account, DateTime.Now.AddDays(-7), DateTime.Now, 0));
var templateData = new Hashtable {{"data", reportData}, {"account", Account}};
foreach (var u in Account.Users.Where(x => x.Notify))
{
TemplatedNotifier.Send(u.UserName, ReplyName, ReplyEmail, insightEmail.TemplateName, templateData);
}
}
}
내가 그러나 나 ', 많은 사람들이 대신 인터페이스의에서 통과 모의 객체 또는 스텁을 사용하는 것이 좋습니다 것이라고 이해 이것이 실제로 얼마나 유익한 지 조금은 혼란 스럽습니다. 이것은 적절한 메소드가 호출되도록 보장하는 것으로 보이는데, 이는 다소 공허감을 느끼게하고, 실제 구현에있어 실제 테스트와도 매우 유사합니다. 궁극적으로 질문은 어떤 가치를 반환하지 않고 단지 당신이 말한 방식대로 구현되었다는 것을 테스트하기 만하면 부작용을 일으키는 방식으로 단위 테스트를 수행합니까?
의 방법을 조롱 할 수있는 방법의 예입니다. 전자 메일 보내기 *는 이미 인터페이스를 구현하는 클래스로 추상화되어 있습니다. 이미 인터페이스를 사용하고 있습니다. IInsightEmailService는 InsightEmails를로드하기위한 저장소입니다. 나는 SystemReplyEmailSpec을 알리미에 넣을 수 있었고, 그것은 의존성을 5로 줄 였지만, 나는 승리를 보지 못했다. 왜 다른 클래스를이 곳에 두는 것이 이미있는 코드를 사용하는 것보다 낫겠습니까? –