通过加强和单元测试调试代码 [英] Stepping through and debugging code in Unit tests
本文介绍了通过加强和单元测试调试代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直没能调试或单步执行单元测试。
下面是我的示例测试代码...
使用系统;
使用System.Text;
使用System.Collections.Generic;
使用System.Linq的;
使用Microsoft.VisualStudio.TestTools.UnitTesting;使用DomainModel.Entities
;
使用DomainModel.Abstract;使用WebUI.Controllers
;
命名空间测试
{
[TestClass中]
公共类PeopleControllerTests
{
静态IPeopleRepository MockPeopleRepository(PARAMS人[]人)
{
变种mockPeopleRepos =新Moq.Mock< IPeopleRepository>();
mockPeopleRepos.Setup(X => x.People).Returns(people.AsQueryable());
返回mockPeopleRepos.Object;
}
[TestMethod的]
公共无效Count_Of_People()
{
IPeopleRepository库= MockPeopleRepository(
新的Person {年龄= 31,性别=男,名称=汤姆},
新的Person {年龄= 25,性别=女,名称=莎莉},
新的Person {年龄= 18,性别=女,名称=约翰}
);
把PeopleController控制器=新把PeopleController(资料库);
变种人= controller.List()ViewData.Model。
VAR peoplelist =人民的IList<人取代;
Assert.AreEqual(3,peoplelist.Count);
}
}
}
解决方案
在使用Microsoft.VisualStudio.TestTools.UnitTesting,去测试在VS 2010的主菜单中,点击子菜单调试 - >在目前的情况下测试
这是我发现的唯一途径。右键单击测试代码,选择运行测试永远不会启动调试程序,即使是在模式=调试。
I have not been able to debug or step through unit test.
Here is my sample test code...
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DomainModel.Entities;
using DomainModel.Abstract;
using WebUI.Controllers;
namespace Tests
{
[TestClass]
public class PeopleControllerTests
{
static IPeopleRepository MockPeopleRepository(params Person[] people)
{
var mockPeopleRepos = new Moq.Mock<IPeopleRepository>();
mockPeopleRepos.Setup(x => x.People).Returns(people.AsQueryable());
return mockPeopleRepos.Object;
}
[TestMethod]
public void Count_Of_People()
{
IPeopleRepository repository = MockPeopleRepository(
new Person { Age = 31, Gender = "Male", Name = "Tom" },
new Person { Age = 25, Gender = "Female", Name = "Sally" },
new Person { Age = 18, Gender = "Female", Name = "John" }
);
PeopleController controller = new PeopleController(repository);
var people = controller.List().ViewData.Model;
var peoplelist = people as IList<Person>;
Assert.AreEqual(3, peoplelist.Count);
}
}
}
解决方案
When using Microsoft.VisualStudio.TestTools.UnitTesting, go to 'Test' in the main menu of VS 2010, click submenu 'Debug' -> 'tests in current context'.
That's the only way I've found. Right-click on the test-code, selecting 'run tests' will never start the debugger, not even when mode = debug.
这篇关于通过加强和单元测试调试代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文