MVC中的测试驱动开发
为什么需要TDD
TDD的由来
它是一种实现代码的方法论而不是一种测试方法
-代码的质量更可靠
TDD的工作方式
-首先编写单元测试
-编写代码用于通过测试
-重构代码
-迭代
编写测试代码
[Test]
public void
StringWithThreeAsReturnsThreeWhenCountingOccurencesOfA() {
//arrange
CharCounter counter = new CharCounter();
//act
int occurrences =
counter.CountOccurrences(“this phrase has three occurrences of a.”, ‘a’);
//assert
Assert.AreEqual(3, occurrences);
}
先写测试
编写应用代码
public int CountOccurrences(string text, char
searchCharacter)
{
int count = 0;
foreach (char character in text) {
if (character == searchCharacter) {
count++;
}}
return count;
}
//代码简单
重构上面的代码
-重构来精简代码
迭代
-重复上面的过程
编写好的单元测试代码
-测试技巧
1) 不要越界
-使代码测试工作帮助函数进行定位
2)默认单元测试
-自动生成
ViewData[“Message”] = “Welcome to ASP.NET MVC!”;
return View();
}
public void Index() {
// Arrange
HomeController controller = new HomeController();
// Act
ViewResult result = controller.Index() as ViewResult;
// Assert
ViewDataDictionary viewData = result.ViewData;
Assert.AreEqual(“Welcome to ASP.NET MVC!”, viewData[“Message”]);
}
3)单元测试要简洁
-只测试自己写的代码
public ActionResult About()
{
return View();
}
[TestMethod]
public void AboutReturnsAboutView()
{
HomeController controller = new HomeController();
ViewResult result = controller.About() as ViewResult;
Assert.AreEqual(“About”, result.ViewName);
} //不能通过
[TestMethod]
public void AboutReturnsAboutView()
{
HomeController controller = new HomeController();
ViewResult result = controller.About() as ViewResult;
//I explicitly want to rely on the framework to set the viewname
Assert.AreEqual(string.Empty, result.ViewName);
}
只测试自己写的代码
使用TDD的好处
-测试目标明确
-测试文档化
-测试安全
-测试提升质量
TDD推荐参考
-Test Driven Development
-Test-Driven Developmenet in Microsoft.net
2011-4-23 12:14 danny
为什么需要TDD
TDD的由来
它是一种实现代码的方法论而不是一种测试方法
-代码的质量更可靠
TDD的工作方式
-首先编写单元测试
-编写代码用于通过测试
-重构代码
-迭代
编写测试代码
[Test]
public void
StringWithThreeAsReturnsThreeWhenCountingOccurencesOfA() {
//arrange
CharCounter counter = new CharCounter();
//act
int occurrences =
counter.CountOccurrences(“this phrase has three occurrences of a.”, ‘a’);
//assert
Assert.AreEqual(3, occurrences);
}
先写测试
编写应用代码
public int CountOccurrences(string text, char
searchCharacter)
{
int count = 0;
foreach (char character in text) {
if (character == searchCharacter) {
count++;
}}
return count;
}
//代码简单
重构上面的代码
-重构来精简代码
迭代
-重复上面的过程
编写好的单元测试代码
-测试技巧
1) 不要越界
-使代码测试工作帮助函数进行定位
2)默认单元测试
-自动生成
ViewData[“Message”] = “Welcome to ASP.NET MVC!”;
return View();
}
public void Index() {
// Arrange
HomeController controller = new HomeController();
// Act
ViewResult result = controller.Index() as ViewResult;
// Assert
ViewDataDictionary viewData = result.ViewData;
Assert.AreEqual(“Welcome to ASP.NET MVC!”, viewData[“Message”]);
}
3)单元测试要简洁
-只测试自己写的代码
public ActionResult About()
{
return View();
}
[TestMethod]
public void AboutReturnsAboutView()
{
HomeController controller = new HomeController();
ViewResult result = controller.About() as ViewResult;
Assert.AreEqual(“About”, result.ViewName);
} //不能通过
[TestMethod]
public void AboutReturnsAboutView()
{
HomeController controller = new HomeController();
ViewResult result = controller.About() as ViewResult;
//I explicitly want to rely on the framework to set the viewname
Assert.AreEqual(string.Empty, result.ViewName);
}
只测试自己写的代码
使用TDD的好处
-测试目标明确
-测试文档化
-测试安全
-测试提升质量
TDD推荐参考
-Test Driven Development
-Test-Driven Developmenet in Microsoft.net
2011-4-23 12:14 danny