任何人都可以给我看一些NSpec被用来测试控制器(以及MVC站点的其他方面)的例子吗?

我开始与NSpec合作,但会喜欢用正确的方式使用它来指定或测试我的控制器的几个指针。基本上几个例子会走很长的路。那里是否有任何示例项目?所有的帮助将不胜感激。

理查德。

0
额外 编辑
意见: 3
嗨,是的,我计划使用specflow进行接受/集成测试,但我的理解是,nspec对于测试组件会更好 - 这似乎是我在RSpec书中读到的常识,等等
额外 作者 richardwhatever,
你看看SpecFlow? specflow.org
额外 作者 danludwig,

1 答案

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();
      };

      context["authenticating"] =() =>
      {
        act =() => result = controller.LogOn(new
        {
          Email = "[email protected]",
          Password = "password",
          RedirectUrl = null as string
        });

        context["user exists"] =() =>
        {
          before =() => 
            user = GivenUser("[email protected]", 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("[email protected]", 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();
      };

      context["user registers"] =() =>
      {
        act =() =>
        {
          result = controller.Register(new
          {
            Email = "[email protected]",
            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("[email protected]");

          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.");
      };
    }
  }
}
0
额外