我正在使用Prism的Confirmation类来询问用户确认。当我进行单元测试时,确认的返回值始终为false。也许我可以公开InteractionRequest的setter。我的代码现在看起来像这样,我的单元测试应该验证调用this.copyService.Execute。
public InteractionRequest<Confirmation> CopyProjectConfirmationRequest { get; private set; }
bool confirmationResult = false;
DialogConfirmation dialogConfirmation = new DialogConfirmation
{
Title = "Copy and Convert Project",
Content = string.Format("This Project was created with Version {0} to be used with currentVersion({1}) it must be converted should it copyed and converted Project", projectVersion, toolVersion),
ConfirmButtonText = "Copy & Convert",
DeclineButtonText = "Cancel"
};
this.CopyProjectConfirmationRequest .Raise(dialogConfirmation, cb => { confirmationResult = cb.Confirmed; });
if (confirmationResult)
{
this.copyService.Execute(this.Model);
}
答案 0 :(得分:8)
解决方案很简单。只需在UnitTest中添加它
sut.CopyProjectConfirmationRequest.Raised += (s, e) =>
{
Confirmation context = e.Context as Confirmation;
context.Confirmed = true;
e.Callback();
};
答案 1 :(得分:1)
您可以尝试模拟copyService并针对模拟进行测试
可以使用Moq
Mock<ICopyService> mockService;
....注入VM或具有CopyProjectConfirmationRequest
属性的类。
mockService.Verify(x => x.Execute(It.Is<Model>(...));
至于CopyProjectConfirmationRequest
我会将该属性公开,并将其存入单元测试中。