lundi 8 août 2016

Substitute property of local variable in Unit Tests

I'm trying to write a simple Unit Test and have a problem with the mocking/substitutes.

The method I want to test looks like this:

public override void Execute(object parameter)
    {
        var openFileDialogViewModel = new OpenFileDialogViewModel
        {
            AddExtension = true,
            DefaultExtension = "xxx",
            Filter = "xxx Files|*.xxx"
        };

        bool result = mFileDialogService.OpenFile(openFileDialogViewModel);

        if (result)
        {
            mDatabaseLoadingService.LoadFile(openFileDialogViewModel.FileName);
        }
    }

For the test I want to set a filename, call the Execute method and check whether the file was loaded or not. In my test I used 'NSubstitute' to tell the 'OpenFile' method of my 'FileDialogService' to return true like this:

mFileDialogService.OpenFile(Arg.Any<IOpenFileDialogViewModel>()).ReturnsForAnyArgs(true)

But how do i set the 'FileName' property of the 'OpenFileDialogViewModel' ?

Thank you for your help.

Aucun commentaire:

Enregistrer un commentaire