Question

How would you go about testing the code below using Moq and Moles if necessary?

I'd like to check that the "TestCommand.RaiseCanExecuteChanged()" command is called once when "SomeEvent" is raised.

The difficulty is that RaiseCanExecuteChanged is a public non-virtual method on Microsoft.Practices.Prism.Commands.DelegateCommandBase and Moles introduces other complications, one of which is it doesn't seem to be able to Mock just a Method without a return value.

using Microsoft.Practices.Prism.Commands;
public class SomeEventClass
{
    public event Action SomeEvent;
}

public class TestClass
{
    public TestClass(SomeEventClass someEventClass)
    {
        TestCommand = new DelegateCommand(() => { /* do stuff */}, CanExecute);
        someEventClass.SomeEvent += () => TestCommand.RaiseCanExecuteChanged();
    }

    private bool CanExecute()
    {
        //Some logic
        return true;
    }

    public DelegateCommand TestCommand { get; private set; }
}
Was it helpful?

Solution

Moles should be able to shim a method that has no return type. Something is preventing Moles from detouring the method.

I suggest using a stub and dependency injection, instead of building a mock (this may take a while) or shimming the call with Moles.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top