c# - Assert.True 未通过测试

标签 c# testing nunit assert

我是第一次使用 NUnit 测试框架,每当我为我的代码运行测试时,它只会通过 Assert.False 命令,而我使用的每个 Assert.True 都会失败。当我运行下面的测试时,只有 TestNotAreYou() 是唯一通过的测试。是我的代码有误还是我有技术问题?

代码:

using System;
using NUnit.Framework;
using System.Collections.Generic;

namespace Week22
{
[TestFixture]
public class Test
{
    private IdentifiableObject id;

    [SetUp]
    public void SetUp()
    {
        id = new IdentifiableObject(new string[] { "fred", "bob" });
    }

    [Test]
    public void TestAreYou()
    {
        IdentifiableObject id = new IdentifiableObject(new string[] { "fred", "bob" });

        Assert.True(id.AreYou("fred"));
        Assert.True(id.AreYou("bob"));
    }

    [Test]
    public void TestNotAreYou()
    {
        IdentifiableObject id = new IdentifiableObject(new string[] { "fred", "bob" });

        Assert.False(id.AreYou("wilma"));
        Assert.False(id.AreYou("boby"));
    }

    [Test]
    public void TestCaseSens()
    {
        IdentifiableObject id = new IdentifiableObject(new string[] { "fred", "bob" });

        Assert.True(id.AreYou("fred"));
        Assert.True(id.AreYou("bob"));
        Assert.True(id.AreYou("Fred"));
        Assert.True(id.AreYou("bOB"));
    }

    [Test]
    public void TestFirstID()
    {
        IdentifiableObject id = new IdentifiableObject(new string[] { "fred", "bob" });

        Assert.True(id.FirstID == "fred");
    }

    [Test]
    public void TestAddID()
    {
        IdentifiableObject id = new IdentifiableObject(new string[] { "fred", "bob" });

        id.AddIdentifier("wilma");
        Assert.True(id.AreYou("fred"));
        Assert.True(id.AreYou("bob"));
        Assert.True(id.AreYou("wilma"));
    }
}
}

正在测试的程序:

using System.Linq;
using System;
using System.Collections.Generic;
using System.Text;

namespace Week22
{
public class IdentifiableObject
{
    private List<string> _identifiers = new List<string>();

    public IdentifiableObject(string[] idents)
    {
        Array.ForEach(idents, s => AddIdentifier("true"));
    }

    public bool AreYou(string id)
    {
        return _identifiers.Contains(id.ToLower());
    }

    public string FirstID
    {
        get
        {
            return _identifiers.First();
        }
    }

    public void AddIdentifier(string id)
    {
        _identifiers.Add(id.ToLower());
    }
}
}

最佳答案

这与单元测试无关。在您的代码中,您正在遍历标识符:

Array.ForEach(idents, s => AddIdentifier("true"));

您要为每个标识符添加字符串“true”。而是添加 s

您可以调试单元测试。然后设置断点并逐步执行代码以检查变量。

关于c# - Assert.True 未通过测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51945619/

相关文章:

c# - 检查异步事件是否仅引发一次

wordpress - 创建重复的 WordPress 安装以进行测试

regex - NUnit 控制台命令行正则表达式不区分大小写?

c# - 如何使用 linq 内连接后获取两个数据表的列

c# - 使用 C# 将数据添加到我的 Sql 数据时出现 "Procedure expects parameter which was not supplied"错误

javascript - 在 Emberjs/handlebars 中为 html 元素定义 ID 失败

c++ - "Catch"单元测试框架 - REQUIRE_THROWS_AS

nunit - NUnit 中 Assert.True 和 Assert.IsTrue 之间的区别?

c# - WPF&MVVM : access to Controls from RelayCommand()

C# 序列化在子类中删除属性?