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

时间:2015-03-09 13:02:54

标签: c# wpf wcf unit-testing moq

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

IMyserviceChannel = MyService;

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

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

然后我在客户端应用程序中添加了一个MS Test项目,我试图使用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的孤立部分?

3 个答案:

答案 0 :(得分:3)

由于您使用NullReferenceExceptionMockBehavior.Strict被mybe抛出。文档说:

  

使这个模拟总是为没有相应设置的调用抛出异常。

也许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());
    }
}

答案 1 :(得分:0)

如果无法访问您的视图模型,我们可以为您提供很多帮助。

但是,这段代码:

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

不设置您的IsesService。如果未在构造函数中设置,则表示IsesService是空引用。您无法在空对象上调用方法。

答案 2 :(得分:-1)

考虑在更高级别的抽象中进行模拟,然后使用您使用的工具进行紧密耦合。

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

以下是一个例子:

Construct testable business layer logic