2

I am unit testing view models and am using mbunit with moq to mock the private method od the class but my requirement is to verify in the assertion part of the test that a some another method is called (which is a dialog box ) present inside the Method which is Under Unit test.

3
  • yes it has an interface but how to mock it perfectly to check the verify call
    – priya
    Mar 13, 2014 at 11:03
  • I have mocked it like this UIServicemock.Setup(u=>u.CloseWindow(It.IsAny<string>(),It.IsAny<Dictionary<string,object>>())); now please suggest how to call verify.
    – priya
    Mar 13, 2014 at 11:07
  • Please check stackoverflow.com/questions/347818/… Mar 13, 2014 at 12:00

2 Answers 2

5

You can easily check a method being called with Moq using the following code:

[TestFixture]
public class UnitTest1
{
    [Test]
    public void TestMethod1()
    {
        // Create a mock of your interface and make the methods verifiable.
        var mock = new Mock<ISomeDependency>();
        mock.Setup(m => m.DoSomething())
            .Verifiable();

        // Setup your class which you expect to be calling the verifiable method
        var classToTest = new SomeClass(mock.Object);
        classToTest.DoWork();

        // Verify the method is called
        mock.Verify(m => m.DoSomething());
    }
}



public class SomeClass
{
    private readonly ISomeDependency _someDependency;

    public SomeClass(ISomeDependency someDependency)
    {
        _someDependency = someDependency;
    }

    public void DoWork()
    {
        _someDependency.DoSomething();
    }
}

public interface ISomeDependency
{
    void DoSomething();
}

public class SomeDependency : ISomeDependency
{
    public void DoSomething()
    {

    }
}

Basicly all you are looking for is the Verifiable in the Arrange part of ur unit test, and the Verify in the Assert part.

0

When something is hard to unit test, that's a "code smell" that you have a design issue.

In this case, the fundamental issue is that you're trying to perform a UI action within your viewmodel. That's bad.

The approach I take when I'm doing MVVM is to raise an event in my viewmodel, e.g. ConfirmationRequired. Then in the view, I hook an event handler up to the event. The event handler in my view is what's responsible for actually displaying the message box.

This is dead simple to unit test. This example is in MSTest, but MBUnit is more or less equivalent.

[TestMethod]
public void User_Confirmation_Is_Requested()
{
    var mre = new ManualResetEvent(false);
    var vm = MyApplicationViewModel();
    ConfirmationRequestedEventArgs actual = null;
    vm.ConfirmationRequired += (sender, args) => {
    {
            actual = args;
            mre.Set();
    };

    vm.DoSomethingThatRequiresConfirmation();
    if (!mre.WaitOne(1000))
    {
        Assert.Fail("The event was never received.");
    }
    Assert.AreEqual("Whatever", actual.SomeProperty ?? string.Empty);
}

The great thing here is that you can even simulate user responses -- let's say your ConfirmationRequestedEventArgs has a property called UserResponse. You can set UserResponse in the event handler of your unit test and then make sure that when the user clicks the "Cancel" button, some other state in the viewmodel changes accordingly.

4
  • 1
    You don't have to move the dialog to the view to make it testable. If you abstract the UI operations occuring in ur ViewModel you can easily mock them out. Let's say my VM has an IDialogManager dependency, this IDialogManager has a "ShowDialog" method... You can easily create a mock or stub to get rid of the actual UI actions. So I don't 100% agree that the issue is with his UI interactions happening in the VM. Displaying a dialogbox can still be Business related, and not just informational (i.e. not showing a sentence, but a view in a popup). Mar 13, 2014 at 11:59
  • I'm not saying you can't, I'm saying that having your viewmodel take explicit, tightly-coupled UI actions is a bad design and suggested an alternative that moves the responsibility of displaying the dialog box to the consumer of the viewmodel, where it belongs. Think of an application that you want to port from one platform to another. Mar 13, 2014 at 12:09
  • My VM's are dependent on an IDialogManager. If you change platform, create a new IDialogManager implementation. There's nothing wrong with doing it the way you are saying, but neither is it wrong to do it in the VM if done correctly. But how does this relate to his question about how to verify a method being called? Mar 13, 2014 at 12:11
  • Btw when unit testing, you are testing units of your application. If you're testing your VM, you should be mocking or stubbing all of his dependencies which are required for that particular test. Never should any UI action get in the way. If it does, there idd is a bad design. Mar 13, 2014 at 12:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.