NUnit 测试失败,即使有参数

NUnit Testing failed even though there are arguments

我正在尝试进行测试以检查视图 returns 字符串是否正确。我是 NUnit 测试的新手,看过一些教程,但我不确定我做错了什么。

using System;

namespace ItemTracker
{

public enum Category{Book,StorageDevice,Stationary};

class Item{

    private string _id;
    private double _price;
    private Category _category;

    public Item(string id, double price, Category category){

        _id=id;
        _price=price;
        _category=category;

    }

    public string ID{

            get{return _id;}
            set{_id=value;}
    }
    public double Price{

            get{return _price;}
            set{_price=value;}
    }

    public Category Category{

            get{return _category;}
            set{_category=value;}
    }

    public string View(){

            if(_category==Category.Book){

                    return "Get ready for the adventure!";

            }

            else if(_category==Category.StorageDevice){

                    return "Data storing in progress";

            }

            else if(_category==Category.Stationary){

                    return "Learn something new with me!";

            }

            else{
                    return "Invalid";
            }

    }


    }


}

这是我的 TestClass.cs 和我已经尝试过的,即将我想要的输出值放入数组:

using NUnit.Framework;
using System;

namespace ItemTracker
{
[TestFixture()]
class testclass{
    [Test()]
    public void Testing(Item[] j){

        j[0]=new Item("B1001",39.90,Category.Book);
        foreach(Item x in j){

            Assert.AreEqual("Get ready for the adventure!",x.View());
        }
    }


}

}

但是我收到一条错误消息:

  Error Message:
   No arguments were provided

确保您的 class Itempublic,我现在看到它的当前访问级别是 internal(默认,当您不提供访问权限时) .

除此之外,您只需要知道不幸的是 MSTest 不支持参数。 另一种选择是使用 Data-driven tests。 另外,您可以查看 this 答案。

作为您案例的当前解决方案,请在您的方法中创建数组,而不是将其作为参数提供。

你应该在测试方法中创建一个数组,否则你的测试将被认为是参数化的(但你没有为参数指定任何属性,如 TestCaseTestCaseSource

[Test]
public void Testing()
{
    var j = new Item[1];
    j[0] = new Item("B1001",39.90,Category.Book);
    foreach(Item x in j)
    {
        Assert.AreEqual("Get ready for the adventure!",x.View());
    }
}