[英]How to execute C# wpf commands via a unit test
我對一般的單元測試和 C# 相當陌生,我試圖驗證一旦一個命令有趣,一個集合具有正確數量的對象。 所以流程應該是:命令執行 -> 方法將對象添加到集合 -> 測試檢查集合是否有對象。 當我嘗試這個時,測試會拋出錯誤“System.InvalidOperationException:調用線程必須是 STA,因為許多 UI 組件都需要這個。”
由於該命令在正常操作中運行良好,我認為我設置測試的方式有些不正確。 我嘗試將 [STAThread] 屬性添加到測試方法中,但仍然出現相同的錯誤。
我要測試的 ViewModel
namespace Space
{
public class SampleViewModel : ObservableObject, IPageViewModel
{
private string _id;
private string _initials;
private ObservableCollection<ISampleObject> _sampleModels;
private ICommand _validateId;
private IdValidator _idValidator;
public ViewModel(string initials)
{
Initials = initials;
}
public string Name
{
get { return "Sample"; }
}
public string Id
{
get
{
return _id;
}
set
{
if (value != _id)
{
_id = value;
OnPropertyChanged("Id");
}
}
}
public ObservableCollection<ISampleObject> SampleModels
{
get
{
if (_sampleModels == null)
{
_sampleModels = new ObservableCollection<ISampleObject>();
OnPropertyChanged("SampleModels");
}
return _sampleModels;
}
set
{
}
}
public IdValidator IdValidator
{
get
{
if (_idValidator == null)
{
_idValidator = new IdValidator();
OnPropertyChanged("IdValidator");
}
return _idValidator;
}
set { }
}
public ICommand ValidateIdCommand
{
get
{
if (_validateId == null)
{
_validateId = new RelayCommand(
param => ValidateId(AliquotId),
Predicate => AliquotId != ""
);
}
return _validateId;
}
}
/**
* <summary> This method checks the entered sample ID and attempts to categorise it
* and call the correct follow-up methods.
* </summary>
**/
private void ValidateId(string Id) // potentailly this should return a bool on successful/unsuccessful validation to make testing easier.
{
if (Id != null)
{
if (IdValidator.IsValidId(Id))
{
switch (IdValidator.ValidateId(AliquotId))
{
case IdType.Sample:
GetSample(Id);
break;
default:
break;
}
}
else
{
MessageBox.Show("Scanned Code not recognised", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
private void GetSample(string id)
{
SampleProvider sampleProvider = new SampleProvider();
SampleModel sampleModel = (SampleModel)sampleProvider .GetById(id);
if (!sampleModel.IsEnumPlate)
{
SampleModels.Add(sampleModel);
}
}
}
}
測試代碼
namespace Space.Tests
{
[TestClass()]
public class SampleViewModelTests
{
ViewModel viewModel = new ViewModel("string") {
Id = "ID string"
};
[TestMethod()]
public void ViewModelTest_SampleModel_Retrieved()
{
viewModel.ValidateIdCommand.Execute(viewModel.Id);
Assert.IsTrue(viewModel.SampleModels.Count > 0);
}
}
}
'''
調用Execute
是調用命令的方式:
viewModel.ValidateIdCommand.Execute(viewModel.Id);
到目前為止,一切都很好。
正如@KlausGütter 評論的那樣,您應該模擬對MessageBox.Show
的調用以使其正常工作。
一種常見且簡單的方法是創建一個實現接口的對話服務:
public interface IDialogService
{
void ShowError(string text, string caption);
}
public class DialogService : IDialogService
{
public void ShowError(string text, string caption) =>
MessageBox.Show(text, caption, MessageBoxButton.OK, MessageBoxImage.Error);
}
...並使用接口注入視圖 model:
private readonly IDialogService _dialogService;
public ViewModel(string initials, IDialogService dialogService)
{
Initials = initials;
_dialogService = dialogService ?? throw new ArgumentNullException(nameof(dialogService));
}
private void ValidateId(string Id) // potentailly this should return a bool on successful/unsuccessful validation to make testing easier.
{
if (Id != null)
{
if (IdValidator.IsValidId(Id))
{
switch (IdValidator.ValidateId(AliquotId))
{
case IdType.Sample:
GetSample(Id);
break;
default:
break;
}
}
else
{
_dialogService.Show("Scanned Code not recognised", "Error");
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.