English 中文(简体)
有没有人能告诉我一些NSpec被用于测试控制器(以及MVC场地的其他方面)的例子?
原标题:Can anyone show me some examples of NSpec being used to test controllers (and other aspects of MVC site)?

我开始和NSPec合作,但希望能够用它来测试或测试我的控制器。 基本上,几个例子可以做很长的路程。 外头有没有什么示范项目吗? 所有帮助都会受到欢迎。

理查 理查 理查 理查 理查 理查 理查 理查

最佳回答

为什么是!下面的测试套件来自一个参考执行, 共有4个独特的 mvc 应用程序, 全部使用NSPec作为测试套件。 绝对要检查它们< a href=> rel=“ noreferrer'> 这里

以下是其中一项测试。 此规格验证登录页和注册页。 用于网站 。

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 = "[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<ViewResult>();
      };

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

问题回答

暂无回答




相关问题
run unit tests and coverage in certain python structure

I have some funny noob problem. I try to run unit tests from commandline: H:PROpyEstimator>python src estpython est_power_estimator.py Traceback (most recent call last): File "src est...

How to unit-test an enterprise symfony project?

I´m working on a huge project at my work. We have about 200 database tables, accordingly a huge number of Models, Actions and so on. How should I begin to write tests for this? My biggest problem ...

Code Coverage Tools & Visual Studio 2008 Pro

Just wondering what people are using for code coverage tools when using MS Visual Studio 2008 Pro. We are using the built-in MS test project and unit testing tool (the one that come pre-installed ...

Unit testing. File structure

I have a C++ legacy codebase with 10-15 applications, all sharing several components. While setting up unittests for both shared components and for applications themselves, I was wondering if there ...

Unit Testing .NET 3.5 projects using MStest in VS2010

There s a bug/feature in Visual Studio 2010 where you can t create a unit test project with the 2.0 CLR. https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=483891&wa=...

Unit Test for Exceptions Message

Is there a simple (Attribute-driven) way to have the following test fail on the message of the exception. [TestMethod()] [ExpectedException(typeof(ArgumentException))] public void ExceptionTestTest() ...

热门标签