views:

14

answers:

1

Why does the last assert in this built-in unit test in the ASP.NET MVC 2 project pass?

//File: AccountControllerTest.cs

    [TestMethod]
    public void ChangePassword_Get_ReturnsView()
    {
        // Arrange
        AccountController controller = GetAccountController();

        // Act
        ActionResult result = controller.ChangePassword();

        // Assert
        Assert.IsInstanceOfType(result, typeof(ViewResult));
        Assert.AreEqual(10, ((ViewResult)result).ViewData["PasswordLength"]);
    }

Shouldn't ViewData["PasswordLength"] be 6? If you look in the project Web.config, minRequiredPasswordLength has the value 6, not 10.

//File: Web.config

<membership>
    <providers>
        <add name="AspNetSqlMembershipProvider" ... minRequiredPasswordLength="6" ... />
    </providers>
</membership>
A: 

Look closely at the method GetAccountController(). It instantiates an AccountController where the IMembershipService points to a MockMembershipService, which has this code:

public int MinPasswordLength {
  get { return 10; }
}

(In the MVC 2 default application, this is defined in AccountControllerTest.cs, lines 290 and 332.)

Levi
You are right - thanks.
hungster