NUnit测试失败,即使有参数

问题描述 投票:0回答:2

我正在尝试进行测试以检查View是否返回正确的字符串。我对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
c# nunit
2个回答
1
投票

您应该在测试方法内部创建一个数组,否则您的测试将被视为参数化的(但是您没有为参数指定任何属性,例如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());
    }
}

1
投票

请确保您的班级Itempublic,我现在看到它的当前访问级别为internal(默认情况下,当您不提供访问权限时。)>]

除此之外,您只需要知道MSTest

不支持参数。另一种选择是使用Data-driven tests。另外,您可以检查this答案。

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

© www.soinside.com 2019 - 2024. All rights reserved.