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

Har*_*raf 5 c# wpf wcf unit-testing moq

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

IMyserviceChannel = MyService;

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

Private void GetPerson()
{
  var selectedPerson = MyService.GetSelectedPerson();
}
Run Code Online (Sandbox Code Playgroud)

然后我在客户端应用程序中添加了一个 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());
    }
Run Code Online (Sandbox Code Playgroud)

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

 string result = articleDataGridViewModel.IsesService.GetArticleBody(1010000008);
Run Code Online (Sandbox Code Playgroud)

所以我在徘徊这是最好的方法还是我最好以某种方式嘲笑适用于测试的 viewModel 的孤立部分?

Tho*_*her 5

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

导致此模拟始终为没有相应设置的调用抛出异常。

也许构造函数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());
}
Run Code Online (Sandbox Code Playgroud)

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

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

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());
    }
}
Run Code Online (Sandbox Code Playgroud)