title: Mocking a ViewModel for unit testing with Moq?
body: <p>New to unit testing. I have a WPF client app hooked into a WCF service via <code>basicHttpbinding</code>. Everything works great. I'm using simple constructor Dependency Injection in my viewModel, passing in an <code>IServiceChannel</code> which I then call me service methods on e.g:</p>
  
  <pre><code>IMyserviceChannel = MyService;
  
  public MyViewModel(IMyServiceChannel myService)
  {
     this.MyService = myService;  
  }
  
  Private void GetPerson()
  {
    var selectedPerson = MyService.GetSelectedPerson();
  }
  </code></pre>
  
  <p>I have then added an MS Test project in the client app and I'm trying to use Moq to mock my service:</p>
  
  <pre><code>  [TestMethod]
      public void GetArticleBody_Test_Valid()
      {
          // Create channel mock
          Mock&lt;IIsesServiceChannel&gt; channelMock = new Mock&lt;IIsesServiceChannel&gt;(MockBehavior.Strict);    
  
          // setup the mock to expect the Reverse method to be called
          channelMock.Setup(c =&gt; 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 =&gt; c.GetArticleBody(1010000008), Times.Once());
      }
  </code></pre>
  
  <p>The test is failing with a <code>System.NullReferenceException. Object reference not set to an instance of an object.</code> at the method invocation here:</p>
  
  <pre><code> string result = articleDataGridViewModel.IsesService.GetArticleBody(1010000008);
  </code></pre>
  
  <p>so I'm wandering whether this is the best way to approach or am I better somehow mocking an isolated part of the viewModel which is applicable to the test?</p>