谁能告诉我NSpec的一些例子被用于测试控制器(MVC和网站的其他方面)? [英] Can anyone show me some examples of NSpec being used to test controllers (and other aspects of MVC site)?
问题描述
我开始与NSpec工作,但会喜欢上用它来规范或测试我的控制器的正确方法有几点建议。基本上几个例子会去很远的路。是否有任何示例项目了吗?所有帮助将是AP preciated。
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.
理查德。
推荐答案
为什么是!下面的测试套件是一个参考实现,有4个,所有使用NSpec作为他们的测试套件,独特的MVC应用。肯定检查出来 href=\"https://github.com/amirrajan/Oak/tree/master/Sample%20Apps\">。
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.");
};
}
}
}
这篇关于谁能告诉我NSpec的一些例子被用于测试控制器(MVC和网站的其他方面)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!