title: Moq: Advanced Mock Setup
body: <p>I am relatively new to Moq and have this complex case to mock and am kind of stuck.  I was hoping an experienced Moq user could advice me on this:</p>
  
  <p>Inside my ViewModel the ctor is calling this load method:</p>
  
  <pre><code>public void LoadCategories()
          {
              Categories = null;
              BookDataService.GetCategories(GetCategoriesCallback);
          }
  </code></pre>
  
  <p>I would like to mock the Service obviously. But since the service's method are void and the return is always through a callback, it gets too complicated for me.</p>
  
  <pre><code>private void GetCategoriesCallback(ObservableCollection&lt;Category&gt; categories)
          {
              if (categories != null)
              {
                  this.Categories = categories;
                  if (Categories.Count &gt; 0)
                  {
                      SelectedCategory = Categories[0];
                  }
                  LoadBooksByCategory();
              }
          }
  </code></pre>
  
  <p>As this was not bad enough, as you can see there is another LoadMethod within called LoadBooksByCategory()</p>
  
  <pre><code>public void LoadBooksByCategory()
          {
              Books = null;
              if (SelectedCategory != null)
                  BookDataService.GetBooksByCategory(GetBooksCallback, SelectedCategory.CategoryID, _pageSize);
          }
  
  private void GetBooksCallback(ObservableCollection&lt;Book&gt; books)
          {
              if (books != null)
              {
                  if (Books == null)
                  {
                      Books = books;
                  }
                  else
                  {
                      foreach (var book in books)
                      {
                          Books.Add(book);
                      }
                  }
  
                  if (Books.Count &gt; 0)
                  {
                      SelectedBook = Books[0];
                  }
              }
          }
  </code></pre>
  
  <p>So now my Mock setup:</p>
  
  <pre><code>bool submitted = false;
                  Category selectedCategory = new Category{CategoryID = 1};
                  ObservableCollection&lt;Book&gt; books;
                  var mockDomainClient = new Mock&lt;TestDomainClient&gt;();
                  var context = new BookClubContext(mockDomainClient.Object);
                  var book = new Book
                  {
                   ...
                  };
  
                  var entityChangeSet = context.EntityContainer.GetChanges();
                  var mockService = new Mock&lt;BookDataService&gt;(context);
  
                  mockService.Setup(s =&gt; s.GetCategories(It.IsAny&lt;Action&lt;ObservableCollection&lt;Category&gt;&gt;&gt;()))
                      .Callback&lt;Action&lt;ObservableCollection&lt;Category&gt;&gt;&gt;(action =&gt; action(new ObservableCollection&lt;Category&gt;{new Category{CategoryID = 1}}));
  
                  mockService.Setup(s =&gt; s.GetBooksByCategory(It.IsAny&lt;Action&lt;ObservableCollection&lt;Book&gt;&gt;&gt;(), selectedCategory.CategoryID, 10))
                      .Callback&lt;Action&lt;ObservableCollection&lt;Book&gt;&gt;&gt;(x =&gt; x(new ObservableCollection&lt;Book&gt;()));
  
  
                  //Act
                  var vm = new BookViewModel(mockService.Object);
  
                  vm.AddNewBook(book);
                  vm.OnSaveBooks();
  
                  //Assert
                  EnqueueConditional(() =&gt; vm.Books.Count &gt; 0);
                  EnqueueCallback(() =&gt; Assert.IsTrue(submitted));
  </code></pre>
  
  <p>As you can see, I have created two Setups for each Service Call, however due their callback and sequential dependency, its highly confusing.</p>
  
  <p>for example, the second service call GetBooksByCategory() would never be called if Selectedcategory property within the viewmodel remains null. But the only thing I can mock here is really just the service injected into the viewmodel. So how am I going to affect that inside the viewmodel through my callback?  :) Does it make sense?</p>
  
  <p>At the very end, I am expecting that the ObservableCollection Books is instantiated and maybe populated with some test data (which I am not doing here, I am happy if its at least instantiated, so that I can test adding a new book to an empty collection)</p>
  
  <p>Thats the idea. Once I can understand this, I think I understand Moq properly. :)</p>