模拟 ViewModel 以使用 Moq 进行单元测试?

Mocking a ViewModel for unit testing with Moq?

单元测试的新手。我有一个 WPF 客户端应用程序通过 basicHttpbinding 连接到 WCF 服务。一切都很好。我在我的 viewModel 中使用简单的构造函数依赖注入,传入一个 IServiceChannel,然后我将其称为服务方法,例如:

IMyserviceChannel = MyService;

public MyViewModel(IMyServiceChannel myService)
{
   this.MyService = myService;  
}

Private void GetPerson()
{
  var selectedPerson = MyService.GetSelectedPerson();
}

然后我在客户端应用程序中添加了一个 MS 测试项目,我正在尝试使用 Moq 来模拟我的服务:

  [TestMethod]
    public void GetArticleBody_Test_Valid()
    {
        // Create channel mock
        Mock<IIsesServiceChannel> channelMock = new Mock<IIsesServiceChannel>(MockBehavior.Strict);    

        // setup the mock to expect the Reverse method to be called
        channelMock.Setup(c => c.GetArticleBody(1010000008)).Returns("110,956 bo/d, 1.42 Bcfg/d and 4,900 bc/d. ");

        // create string helper and invoke the Reverse method
        ArticleDataGridViewModel articleDataGridViewModel = new ArticleDataGridViewModel(channelMock.Object);
        string result = channelMock.GetArticleBody(1010000008);
        //Assert.AreEqual("cba", result);

        //verify that the method was called on the mock
        channelMock.Verify(c => c.GetArticleBody(1010000008), Times.Once());
    }

测试失败并在此处的方法调用处显示 System.NullReferenceException. Object reference not set to an instance of an object.

 string result = articleDataGridViewModel.IsesService.GetArticleBody(1010000008);

所以我在想这是否是最好的方法,或者我是否更好地以某种方式模拟适用于测试的 viewModel 的孤立部分?

考虑在更高的抽象层次上进行模拟,然后再考虑与您使用的工具的紧密耦合。

也许您的视图模型应该依赖于服务而不是您使用的工具的细节(即 IIsesServiceChannel)。

这是一个例子:

Construct testable business layer logic

NullReferenceException 是我抛出的,因为你使用了 MockBehavior.Strict。文档说:

Causes this mock to always throw an exception for invocations that don't have a corresponding setup.

可能 ArticleDataGridViewModel 的构造函数调用了您尚未设置的服务的其他方法。 另一个问题是,您正在直接调用模拟方法。相反,您应该调用视图模型的方法,该方法调用此方法。

[TestMethod]
public void GetArticleBody_Test_Valid()
{
    // Create channel mock
    Mock<IIsesServiceChannel> channelMock = new Mock<IIsesServiceChannel>();    

    // setup the mock to expect the Reverse method to be called
    channelMock.Setup(c => c.GetArticleBody(1010000008)).Returns("110,956 bo/d, 1.42 Bcfg/d and 4,900 bc/d. ");

    // create string helper and invoke the Reverse method
    ArticleDataGridViewModel articleDataGridViewModel = new ArticleDataGridViewModel(channelMock.Object);
    string result = articleDataGridViewModel.MethodThatCallsService();
    //Assert.AreEqual("cba", result);

    //verify that the method was called on the mock
    channelMock.Verify(c => c.GetArticleBody(1010000008), Times.Once());
}

除此之外,我认为你的方法没有问题。也许视图模型违反了单一职责原则并且做了比它应该做的更多的事情,但是根据您的代码示例很难判断。

编辑:这是一个完整的例子,说明如何测试这样的东西:

public interface IMyService
{
    int GetData();
}

public class MyViewModel
{
    private readonly IMyService myService;

    public MyViewModel(IMyService myService)
    {
        if (myService == null)
        {
            throw new ArgumentNullException("myService");
        }

        this.myService = myService;
    }

    public string ShowSomething()
    {
        return "Just a test " + this.myService.GetData();
    }
}

class TestClass
{
    [TestMethod]
    public void TestMethod()
    {
        var serviceMock = new Mock<IMyService>();
        var objectUnderTest = new MyViewModel(serviceMock.Object);

        serviceMock.Setup(x => x.GetData()).Returns(42);

        var result = objectUnderTest.ShowSomething();

        Assert.AreEqual("Just a test 42", result);
        serviceMock.Verify(c => c.GetData(), Times.Once());
    }
}

如果无法访问您的视图模型,我们只能为您提供这么多帮助。

然而,这段代码:

Mock<IIsesServiceChannel> channelMock = new Mock<IIsesServiceChannel>(MockBehavior.Strict);
...
ArticleDataGridViewModel articleDataGridViewModel = new ArticleDataGridViewModel(channelMock.Object);
...
string result = articleDataGridViewModel.IsesService.GetArticleBody(1010000008);

不设置您的 IsesService。如果它没有在您的构造函数中设置,则意味着 IsesService 是空引用。您不能在空对象上调用方法。