WPF(Prism.Wpf)の開発で、View に MahApps.Metro を使用しています。
プログレスダイアログも MahApps.Metro を使用しているのですが、ViewModelのテストを行う時に ProgressDialogController を new() すると、
ProgressDialogController はpublicのコンストラクタが用意されていないため new() できず、試験できません。
試験するにはどうすればよいでしょうか。
ダイアログは、IDialogServiceというサービスにしています。
c#
1public interface IDialogService 2{ 3 Task<MessageDialogResult> ShowMessageAsync(string title, string message, MessageDialogStyle style = MessageDialogStyle.Affirmative, MetroDialogSettings settings = null); 4 Task<ProgressDialogController> ShowProgressAsync(string title, string message, bool isCancelable = false, MetroDialogSettings settings = null); 5} 6 7public class MessageDialog : IDialogService 8{ 9 public MetroWindow MainWindow { get; set; } 10 11 public MessageDialog(IUnityContainer container) 12 { 13 this.MainWindow = container.Resolve<Window>("Shell") as MetroWindow; 14 } 15 16 public async Task<MessageDialogResult> ShowMessageAsync(string title, string message, 17 MessageDialogStyle style = MessageDialogStyle.Affirmative, MetroDialogSettings settings = null) 18 { 19 return await this.MainWindow.ShowMessageAsync(title, message, style, settings); 20 } 21 22 public async Task<ProgressDialogController> ShowProgressAsync( 23 string title, string message, bool isCancelable = false, MetroDialogSettings settings = null) 24 { 25 return await this.MainWindow.ShowProgressAsync(title, message, isCancelable, settings); 26 } 27} 28
c#
1public class ViewModel 2{ 3 private IDialogService _dialogService; 4 : 5 : 6 7 public ViewModel(IDialogService dialogService) 8 { 9 _dialogService = dialogService; 10 } 11 12 public async Task LoadDataAsync() 13 { 14 var controller = await _dialogService.ShowProgressAsync( 15 "読み込み中", "データを読み込んでいます...", true); 16 17 controller.Canceled += async (s, e) => 18 { 19 await _model.CancelLoadAsync(); 20 await controller.CloseAsync(); 21 }; 22 23 await _model.LoadAsync(); 24 25 if (controller.IsOpen && !controller.IsCanceled) 26 { 27 await controller.CloseAsync(); 28 } 29 } 30}
テストメソッドで new ProgressDialogController() したいですがinternal のため生成できず、試験できません。
c#
1using Microsoft.VisualStudio.TestTools.UnitTesting; 2using Moq; 3 4[TestClass] 5public class CheckerMasterViewModelTest 6{ 7 : 8 : 9 10 [TestMethod] 11 public async Task LoadDataAsyncTest() 12 { 13 var dialogService = new Mock<IDialogService>(); 14 15 dialogService.Setup(x => x.ShowProgressAsync("読み込み中", "データを読み込んでいます...", true, null)) 16 .ReturnsAsync(() => 17 // ProgressDialogController()は、internalのため、newできない。 18 new ProgressDialogController() 19 { 20 }); 21 } 22}
あなたの回答
tips
プレビュー