title: Testing ASP.NET MVC View Model
body: <p>I'm using Nunit and Moq to test my asp.net mvc solution. Is this a good way to test that the model passed to the view is a correct object/collection?</p>
  
  <pre><code>[Test]
  public void Start_Page_Should_Display_Posts()
  {
      var posts = new List&lt;Post&gt; {new Post {Id = 1}, new Post {Id = 2}};
  
      var mock = new Mock&lt;IRepository&gt;();
      mock.Setup(x =&gt; x.FindAll&lt;Post&gt;()).Returns(posts.AsQueryable());
  
      var controller = new PostsController(mock.Object);
      var result = controller.Index(null) as ViewResult;
      var viewModel = controller.ViewData.Model as IEnumerable&lt;Post&gt;;
  
      Assert.IsNotNull(result);
      Assert.IsTrue(viewModel.Count() == mock.Object.FindAll&lt;Post&gt;().Count());
  }
  </code></pre>
  
  <p>I understand that this kind of tests the framework, but hopefully you'll get my point. Can I trust this test?</p>
  
  <p>Currently i'm a bit tired so don't hesitate to ask for an elaboration.</p>
  
  <p>Thanks</p>