简体   繁体   中英

How to write the nunit test case for the following

How to write the Nunit test case for the following code snippet? CreateFolder() is a function to create a new folder in the tree view under the selected folder and CanCreateFolder() is a function to check whether a folder can be created.

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

What all things should I add in the following test case?

[Test]
public void CreateFolderCommandMainVMTest()
{
    MainVm mainVM = new MainVm();

    RelayCommand command = (RelayCommand)mainVM.CreateFolderCommand;
    bool canCreateFolder = command.CanExecute(mainVM);
    Assert.Equals(canCreateFolder, true);
}

CanExecute is looking for a FolderItem , yet in the test you are passing the view model as the parameter. You need to test the can execute with a FolderItem instance.

Given that no information was given about that type in the example the following makes an assumption that FolderItem has a default constructor.

[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);
}

The above should pass given the provided example in the OP

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM