嗨,我正在整理我的网站,虽然它非常简单,但我决定尽可能多地测试它。使用只编写有用的测试的精神,这些测试可以解释我可以想象发生的情况(重命名脚本或 css 文件等)
我正在使用 Steve Sanderson 的 MVC 集成测试框架,我的测试如下。
我的问题是双重的,这个级别的测试是否“太多”,如果不是,您还能想到哪些其他场景(与开发人员相关,例如重命名或其他任何内容)。
using System.Web;
using System.Web.Mvc;
using MvcIntegrationTestFramework.Hosting;
using NUnit.Framework;
using website.Properties;
namespace website.tests
{
[TestFixture]
public class HomeControllerIndexTests
{
[TestFixtureSetUp]
public void Setup()
{
appHost = AppHost.Simulate("Website");
}
[Test]
public void HomeControllerIndexReturnsTheIndexView()
{
appHost.Start(session =>
{
var result = session.Get("/Home/Index");
Assert.AreEqual("Index", ((ViewResult)result.ActionExecutedContext.Result).ViewName);
});
}
[Test]
public void HomeControllerIndexReturnsCorrectRouteData()
{
appHost.Start(session =>
{
var result = session.Get("/Home/Index");
Assert.AreEqual("Home", result.ActionExecutedContext.RouteData.Values["controller"]);
});
}
[Test]
public void HomeControllerIndexReturnsViewResult()
{
appHost.Start(session =>
{
var result = session.Get("/Home/Index");
Assert.IsInstanceOf(typeof(ViewResult), result.ActionExecutedContext.Result);
});
}
[Test]
public void HomeControllerIndexReturnsNoError()
{
appHost.Start(session =>
{
var result = session.Get("/Home/Index");
Assert.IsNull(result.ResultExecutedContext.Exception);
});
}
[Test]
public void HomeControllerIndexReturnsViewWithSiteCssFile()
{
appHost.Start(session =>
{
var result = session.Get("/Home/Index");
Assert.IsTrue(result.ResponseText.Contains("/Content/Site.css"));
});
}
[Test]
public void HomeControllerIndexReturnsViewWithCorrectTitle()
{
appHost.Start(session =>
{
var result = session.Get("/Home/Index");
Assert.IsTrue(result.ResponseText.ToLowerInvariant().Contains("<title>{ me: danielelliott.info(); }</title>"));
});
}
[Test]
public void HomeControllerIndexReturnsViewContainingBanner()
{
appHost.Start(session =>
{
var result = session.Get("/Home/Index");
var expected = HttpUtility.HtmlEncode(Resources.SiteName);
Assert.IsTrue(result.ResponseText.Contains(expected));
});
}
[Test]
public void HomeControllerIndexViewIncludesBioParagraph()
{
appHost.Start(session =>
{
var result = session.Get("/Home/Index");
var expected = HttpUtility.HtmlEncode(Resources.Bio.ToLowerInvariant());
Assert.IsTrue(result.ResponseText.ToLowerInvariant().Contains(expected));
});
}
[Test]
public void HomeControllerIndexViewIncludesServicesParagraph()
{
appHost.Start(session =>
{
var result = session.Get("/Home/Index");
var expected = HttpUtility.HtmlEncode(Resources.Services.ToLowerInvariant());
Assert.IsTrue(result.ResponseText.ToLowerInvariant().Contains(expected));
});
}
[Test]
public void HomeControllerIndexViewIncludesHistoryParagraph()
{
appHost.Start(session =>
{
var result = session.Get("/Home/Index");
var expected = HttpUtility.HtmlEncode(Resources.History.ToLowerInvariant());
Assert.IsTrue(result.ResponseText.ToLowerInvariant().Contains(expected));
});
}
private AppHost appHost;
}
}