Search code examples
asp.net-mvcasp.net-mvc-3unit-testingbddnspec

Can anyone show me some examples of NSpec being used to test controllers (and other aspects of MVC site)?


I'm starting working with NSpec but would love a few pointers on the right way to use it to spec or test my controllers. Basically a few examples would go a very long way. Are there any example projects out there? All help would be appreciated.

Richard.


Solution

  • Why yes! The tests suite that follows is from a reference implementation, there are 4 unique mvc applications that all use NSpec as their test suite. Definitely check them out here.

    Here is one of the tests. This specification verifies a login page and a registration page. For a website.

    using System;
    using System.Linq;
    using NSpec;
    using BorrowedGames.Controllers;
    using System.Web.Mvc;
    using System.Collections.Generic;
    
    namespace BorrowedGames.Tests.Controllers
    {
      class describe_AccountController : _borrowed_games
      {
        AccountController controller;
    
        dynamic user;
    
        bool authenticated;
    
        void before_each()
        {
          controller = new AccountController();
    
          controller.Authenticate = s =>
          {
            authenticated = true;
    
            SetCurrentUser(controller, Users.ForEmail(s).Id);
          };
        }
    
        void logging_in()
        {
          context["requesting login page"] = () =>
          {
            act = () => result = controller.LogOn();
    
            it["returns login page"] = () => 
              (result as object).should_cast_to<ViewResult>();
          };
    
          context["authenticating"] = () =>
          {
            act = () => result = controller.LogOn(new
            {
              Email = "user@example.com",
              Password = "password",
              RedirectUrl = null as string
            });
    
            context["user exists"] = () =>
            {
              before = () => 
                user = GivenUser("user@example.com", null, "password");
    
              it["authenicates user"] = () =>
                authenticated.should_be_true();
    
              it["redirects to home page"] = () => 
                (result.Url as string).should_be("/");
    
              it["sets user in session"] = () =>
                (controller.UserId()).should_be((decimal)user);
            };
    
            context["user exists, password doesn't match"] = () =>
            {
              before = () => 
                GivenUser("user@example.com", null, "other");
    
              it["returns invalid login"] = () => 
                (result.ViewBag.Flash as string).should_be("Login failed.");
            };
    
            context["user does not exist"] = () =>
            {
              it["returns invalid login"] = () => 
                (result.ViewBag.Flash as string).should_be("Login failed.");
            };
          };
        }
    
        void registering_for_site()
        {
          context["requesting registration page"] = () =>
          {
            act = () => result = controller.Register();
    
            it["returns view"] = () => 
              (result as object).should_cast_to<ViewResult>();
          };
    
          context["user registers"] = () =>
          {
            act = () =>
            {
              result = controller.Register(new
              {
                Email = "user@example.com",
                Password = "password",
                PasswordConfirmation = "password"
              });
    
              user = Users.All().First().Id;
            };
    
            it["logs in user"] = () => 
              (result.Url as string).should_be("/");
    
            it["authenticates user"] = () => 
              authenticated.should_be_true();
    
            it["sets user in session"] = () =>
              ((decimal)controller.UserId()).should_be((decimal)user);
    
            context["user name is taken"] = () =>
            {
              before = () => GivenUser("user@example.com");
    
              it["return error stating that user name is taken"] = () =>
                (result.ViewBag.Flash as string).should_be("Email is unavailable.");
            };
          };
    
          context["registration is invalid"] = () =>
          {
            act = () => result = 
              controller.Register(new 
              { 
                Email = default(string), 
                Password = default(string) 
              });
    
            it["returns error stating that email is required."] = () =>
              (result.ViewBag.Flash as string).should_be("Email is required.");
          };
        }
      }
    }