I have an application that I am building unit tests for. For certain operations, I use ApplicationCommands
(eg. ApplicationCommands.New
). Is there an easy way to call CanExecute
and Execute
on a routed UI command in the unit test? I thought about implementing a mock IInputElement
, but that seems like a lot of work. Is there a better way?
[TestMethod]
public void NewDocument()
{
Assert.IsTrue(ApplicationCommands.New.CanExecute(null, mockTarget));
ApplicationCommands.New.Execute(null, mockTarget);
Assert.IsTrue(workspace.OpenDocuments.Count == 1);
}
It looks like this is very similar to this question why-does-my-command-canexecute-always-return-false-in-unit-test? Does anyone know of a way to execute the routed ui command without the ui actually being there?
I have the command and command binding, but I don't know how to create the command source and command target.
You should be testing the actual
ICommand
class that responds to the routed command. If this routed command is implemented just by event handlers in one of you code-behind files, then I'd suggest pulling anICommand
out of those and testing that.Testing that the command is routed properly is not a unit test you should be writing. Instead that should be an integration test that could be done with coded UI testing via Visual Studio (or some other GUI test framework).