【发布时间】:2021-09-27 23:28:44
【问题描述】:
我正在尝试对显示对话框的命令的命令执行进行单元测试,但是我不断收到 NullReferenceException,我不知道为什么。任何建议将不胜感激。
view sn-p使用的viewmodel:
public class ConvertFileDialogViewModel:IConvertFileDialogViewModel
{
private string _inputFolderPath;
public DelegateCommand SelectInputFolderCommand { get;set; }
public ConvertFileDialogViewModel()
{
SelectInputFolderCommand = new DelegateCommand(SelectInputFolderDialog);
}
public string InputFolderPath
{
get => _inputFolderPath;
set
{
_inputFolderPath = value;
OnPropertyChanged(nameof(InputFolderPath));
}
}
public void SelectInputFolderDialog()
{
using (var fbd = new FolderBrowserDialog())
{
DialogResult result = fbd.ShowDialog();
if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
{
InputFolderPath = fbd.SelectedPath;
}
}
}
}
接口sn-p:
public interface IConvertInterviewDialogViewModel
{
string InputFolderPath { get; set; }
DelegateCommand SelectInputFolderCommand { get;set; }
}
单元测试 sn-p:
[TestMethod]
public void SelectInputFolderCommandTest()
{
var model = MockRepository.GenerateMock<IConvertFileDialogViewModel>();
model.SelectInputFolderCommand.Execute();
model.AssertWasCalled(vm=>vm.SelectInputFolderCommand);
}
错误:
ConvertInterviewDialogViewModelTests.SelectInputFolderCommandTest threw exception: System.NullReferenceException: Object reference not set to an instance of an object.
【问题讨论】:
标签: c# wpf unit-testing mvvm rhino-mocks