如何为以下代码段编写Nunit测试用例?
CreateFolder()
是在所选文件夹下的树视图中创建新文件夹的功能,CanCreateFolder()
是检查是否可以创建文件夹的功能。
public ICommand CreateFolderCommand
{
get
{
if (createFolderCommand == null)
{
createFolderCommand = new RelayCommand(CreateFolder, CanCreateFolder);
}
return createFolderCommand;
}
}
private bool CanCreateFolder(object parameter)
{
if (parameter is FolderItem)
{
return true;
}
return false;
}
#endregion
我应该在以下测试用例中添加哪些内容?
[Test]
public void CreateFolderCommandMainVMTest()
{
MainVm mainVM = new MainVm();
RelayCommand command = (RelayCommand)mainVM.CreateFolderCommand;
bool canCreateFolder = command.CanExecute(mainVM);
Assert.Equals(canCreateFolder, true);
}
答案 0 :(得分:0)
CanExecute
正在寻找FolderItem
,但在测试中,您将视图模型作为参数传递。您需要使用FolderItem
实例测试can执行。
鉴于在示例中没有给出关于该类型的信息,下面假设FolderItem
具有默认构造函数。
[Test]
public void CreateFolderCommandMainVMTest() {
//Arrange
var mainVM = new MainVm();
var foldeItem = new FolderItem();
var command = (RelayCommand)mainVM.CreateFolderCommand;
var expected = true;
//Act
bool canCreateFolder = command.CanExecute(folderItem);
//Assert
Assert.Equals(expected, canCreateFolder);
}
上面应该通过OP中提供的示例