1

I am trying to do a test to check the View returns the correct string. I am very new to NUnit testing, and have looked into a few tutorials but I am not sure on what im doing wrong.

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

    }


    }


}

This is my TestClass.cs and what I have already tried, which is to put the values of the output which I want to the array:

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


}

}

However im getting an error message:

  Error Message:
   No arguments were provided
Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66
Master Irfan Elahee
  • 165
  • 1
  • 1
  • 14
  • 1
    Look [here](https://github.com/nunit/docs/wiki/Parameterized-Tests) to read about parameterized tests. You need to add an annotation (attribute) to specify how your arguments should be passed. Why not removing the argument? You don't need it. Define your array inside? – Oguz Ozgul Apr 18 '20 at 18:28
  • @OguzOzgul wow, that just saved me, thank you – Master Irfan Elahee Apr 18 '20 at 18:37

2 Answers2

1

Make sure that your class Item is public, I see it's current access level is internal(default, when you don't provide access) now.

Other than that, you just need to know that unfortunately parameters are not supported in MSTest. Another option would be to use Data-driven tests. Also, you can check this answer.

As a current solution for your case, please create the array inside your method, instead of providing it as a parameter.

Arsen Khachaturyan
  • 7,904
  • 4
  • 42
  • 42
1

You should create an array inside test method, otherwise your test will be considered as parametrized (but you didn't specify any attributes for the parameters, like TestCase or TestCaseSource)

[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());
    }
}
Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66