c# - 在单元测试中单步执行和调试代码

标签 c# visual-studio unit-testing mstest

我无法调试或单步执行单元测试。

这是我的示例测试代码...

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

    }
}

最佳答案

使用 Microsoft.VisualStudio.TestTools.UnitTesting 时,转到 VS 2010 主菜单中的“测试”,单击子菜单“调试”->“在当前上下文中测试”。

右键单击测试代码并选择“运行测试”将永远不会启动调试器,即使在模式 = 调试时也是如此。

关于c# - 在单元测试中单步执行和调试代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4103712/

相关文章:

java - 类似于Java中C#的Process.Close事件?

c# - 拥有一个大的工作流程还是几个更小的具体工作流程更好?

c# - 如何将数字与自定义 2char BaseX 相互转换? (又名 : How to do Azure Table property compression)

c++ - 为什么新的 Visual Studio 将字符串文字作为指向常量的指针?

python - 为多个 Python 文件编写单元测试

c# - 如何检查我的应用程序是否安装在客户端机器上?

visual-studio - 使用 PowerShell 和 TFS.exe 更新和删除项目中的文件

MySQL Visual Studio 2019 集成

.net - 在 System.Reflection 中使用 Moles

ios - 为什么某些功能覆盖率已经是 100% xcode 9