DCSIMG
January 2009 - Posts - new { Name = ”Shay Jacoby” }

new { Name = ”Shay Jacoby” }

Maximum separation, minimum Dependencies, No Injections.

January 2009 - Posts

MVC Framework RC1 - Moq vs. TypeMock Isolator Controller test

A new version of MVC Framework is just released and has some cool improvements:

The Views have no ".cs" (code behind like) files.

You could generate CRUD Views from VS.

HTML Form fields could be Type Safe, You could Bind class property to form field.

The Controller action behavior is very testability, example:

The Moq example from ScottGu's post:

 

[TestMethod]

   public void Display_Message_Authenticated()

   {

      // Arrange

      HomeController controller = new HomeController();

 

      Mock<ControllerContext> mockControllerContext = new Mock<ControllerContext>();

      mockControllerContext.Setup(c => c.HttpContext.Request.IsAuthenticated).Returns(true);

      mockControllerContext.Setup(c => c.HttpContext.User.Identity.Name).Returns("test");

      controller.ControllerContext = mockControllerContext.Object;

 

      // Act

      ViewResult result = controller.DisplayMessage() as ViewResult;

 

      // Assert

      Assert.IsNotNull("DisplayMessage() should have returned a ViewResult.");

 

 

      ViewDataDictionary viewData = result.ViewData;

      Assert.AreEqual("Hello test", viewData["Message"], "Message incorrect");

    }

 

Here's a test I wrote using TypeMock Isolator:

 

[TestMethod]

[Isolated]

public void Display_Message_Authenticated_using_TypeMock()

{

  // Arrange

  HomeController controller = new HomeController();

 

  ControllerContext mockControllerContext = Isolate.Fake.Instance<ControllerContext>();

  Isolate.WhenCalled(() => mockControllerContext.RequestContext.HttpContext.

Request.IsAuthenticated).WillReturn(true);

 

  Isolate.WhenCalled(() => mockControllerContext.RequestContext.HttpContext.

User.Identity.Name).WillReturn("test");

  controller.ControllerContext = mockControllerContext;

 

 

   // With TypeMock You can Mock HttpContext directley without mocking the ControllerContext:

   Isolate.WhenCalled(() => HttpContext.Current.Request

.IsAuthenticated).WillReturn(true);

 

   // Act

   ViewResult result = controller.DisplayMessage() as ViewResult;

   Isolate.WhenCalled(() => mockControllerContext.RequestContext

.HttpContext.Request.IsAuthenticated).WillReturn(true);

  // Assert

  Assert.IsNotNull("DisplayMessage() should have returned a ViewResult.");

 

 

 

    ViewDataDictionary viewData = result.ViewData;

    Assert.AreEqual("Hello test", viewData["Message"], "Message incorrect");

}

 

I attached both test methods as attachement to this post.

 

Cool!

Typemock Isolator 5.2 release

Hi, There's a new release of Typemock Isolator 5

This tool is excellent and helps to improve your unit tests.

 

 

 


Programming Visual Basic applications?

Typemock have released a new version of their unit testing tool, Typemock Isolator 5.2.
This version includes a new friendly VB.NET API which makes Isolator the best Isolation tool for unit testing A Visual Basic (VB) .NET application.
Isolator now allows unit testing in VB or C# for many ‘hard to test’ technologies such as SharePoint, ASP.NET MVC, partial support for Silverlight, WPF, LINQ, WF, Entity Framework, WCF unit testing and more.

Note that the first 25 bloggers who blog this text in their blog and tell us about it, will get a Free Full Isolator license (C#, VB, and Sharepoint included - worth $139 !!!). If you post this in a VB.NET dedicated blog, you'll get a license automatically (even if more than 25 submit) during the first week of this announcement.

Go ahead, click the following link for more information on how to get your free license.