DCSIMG
MVC Framework RC1 - Moq vs. TypeMock Isolator Controller test - new { Name = ”Shay Jacoby” }

new { Name = ”Shay Jacoby” }

Maximum separation, minimum Dependencies, No Injections.

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!

תוכן התגובה

Eli Lopian כתב/ה:

Thanks for the sample.

I would think that if you fake HttpContext, then you don't have to fake the Controller Context.

# January 28, 2009 8:18 PM

שי יעקובי כתב/ה:

Thanks Eli,

In the code comment "With TypeMock You can..." I meant that HttpContext faking could replace the Controller Context but I probably wasn't clear enough (wasn't clear at all).

# January 28, 2009 10:29 PM

nick_raccot כתב/ה:

# May 18, 2009 9:58 AM
שלח תגובה

(שדה חובה)  

(שדה חובה)  

(אופציונלי)

(שדה חובה) 

Please add 2 and 5 and type the answer here:


Enter the numbers above: