Reputation: 157
I'm developing an application in WPF, with MVVMLight framework.
I'm trying to make unit tests (i'm novice in that). So I try to simulate my view by subscribing to the CanExecuteChanged event on my command and verify that it is correctly called. But when I do that it is never called, even if I call the RaiseCanExecuteChanged method.
Here is a very simple sample:
bool testCanExec = false;
var testCmd = new RelayCommand(
execute: () => { System.Diagnostics.Debug.WriteLine($"Execute call"); },
canExecute: () => { System.Diagnostics.Debug.WriteLine($"CanExecute call"); return testCanExec; }
);
testCmd.CanExecuteChanged += ((sender, args) => { System.Diagnostics.Debug.WriteLine($"CanExecuteChanged call"); });
testCanExec = true;
testCmd.RaiseCanExecuteChanged(); // <= nothing in output
testCmd.Execute(null); // <= output: "CanExecute call", "Execute call"
What I really can't understand is that it seems to work with my button. I don't know how but it enables and disables properly.
Thank's for your help.
Upvotes: 1
Views: 1084
Reputation: 169190
The RelayCommand
's RaiseCanExecuteChanged
method simply calls CommandManager.InvalidateRequerySuggested()
which has no effect in the context of a unit test: https://github.com/lbugnion/mvvmlight/blob/b23c4d5bf6df654ad885be26ea053fb0efa04973/V3/GalaSoft.MvvmLight/GalaSoft.MvvmLight%20(NET35)/Command/RelayCommandGeneric.cs
..because there are no controls that have subscribed to the CommandManager.RequerySuggested
event.
Also, you are generally not really supposed to write unit tests that test the functionality of a third-party framework. You should probably focus on testing your own custom functionality.
But if you want to test the CanExecute
method, you should simply call it instead of raising the CanExecuteChanged
event:
bool b = testCmd.CanExecute(null);
Upvotes: 2