开发者

First Unit Tests! ASP.NET MVC with Repositories causing errors

I am very new to Unit Testing, so I am starting on my first set of tests today. I am using the Library JustMock from Telerik. Though any unit testing information is good. I am having a bit of trouble with an interface service that passes through my method. Below is my MembershipController.Register(model) method...

    [CaptchaValidator]
    [HttpPost]
    public ActionResult Register(Models.Membership.Registration model)
    {
        // just for good mesure, truncate any spaces that are given 
        System.Text.RegularExpressions.Regex.Replace(model.Email, @"\s", "");

        if (ModelState.IsValid)
        {
            // Attempt to register the User and return any applicable status that has to do
            // with the result.
            var createStatus = membershipService.RegisterMember(model.Email, model.Password);

            // if the member is able to be created successfully, log them in now and begin the
            // authentication portion of the regis开发者_如何学编程tration, otherwise, display the registration
            // errors and return to the view.
            if (createStatus == Membership.MemberCreateStatus.Success)
            {
                formsAuthentication.SignIn(model.Email, false /* createPersistentCookie */);
                return RedirectToAction("Success");
            }
            else
            {
                ModelState.AddModelError("", Membership.Validator.ErrorCodeToString(createStatus));
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

And here is the paltry test I am trying to run...

    [TestMethod]
    public void Register_Post_ReturnsRedirectOnSuccess()
    {
        // Arrange
        var controller = Mock.Create<Web.Controllers.MembershipController>();

        var repository = Mock.Create<Membership.IMembershipService>();

        Mock.Arrange(() => repository.RegisterMember("acceptible@email.com", "acceptiblePassword")).Returns(Membership.MemberCreateStatus.Success);

        // Model
        var model = new Web.Models.Membership.Registration
        {
            Email = "acceptible@email.com",
            Password = "acceptiblePassword",
            ConfirmPassword = "acceptiblePassword"
        };

        // Act
        var result = controller.Register(model);

        // Assert
        Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult));

    }

The test fails because membershipService is resolving as null. I'm not sure what to do here. This is my first forray into the Unit Testing aspect of ASP.NET MVC. Can anyone give me some advice?

I am using Ninject to inject IMembershipService through the Constructor. It is implemented by the class MembershipService. The code runs fine when I run it, but the unit tests fail.


I don't see you passing repository anywhere into your controller. Normally you would have IMembershipService as a parameter in your controller's constructor that you can then pass in when needed or use MVC's Service Locator to grab the Ninject instance and pass it in.

:)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜