C# 在NUnit测试中将数组传递给参数

C# 在NUnit测试中将数组传递给参数,c#,nunit,C#,Nunit,我正在学习C#,并尝试将数组作为参数传递(这在我的代码中很好,但我似乎无法在NUnit中为它创建TestCase。我的文件是: Walk.cs: using System; namespace TenMinWalk { public class Walk { static void Main(string[] args) { } public string Walking(Array[] newWalk)

我正在学习C#,并尝试将数组作为参数传递(这在我的代码中很好,但我似乎无法在NUnit中为它创建
TestCase
。我的文件是:

Walk.cs:

using System;

namespace TenMinWalk
{
    public class Walk
    {
        static void Main(string[] args)
        {

        }

        public string Walking(Array[] newWalk)
        {
            if (newWalk.Length == 10)
            {
                return "true";
            }
            return "false";
        }

    }
}

WalkTests.cs:

using NUnit.Framework;
using TenMinWalk;

namespace TenMinWalkTests
{
    public class TenMinWalkTests
    {
        [SetUp]
        public void Setup()
        {
        }

        [Test]
        public void WalkMustOnlyLast10Minutes()
        {
            Walk walk = new Walk();
            string actual = walk.Walking(['w', 's', 'e', 'e', 'n', 'n', 'e', 's', 'w', 'w']);
            string expected = "true";
            Assert.AreEqual(actual, expected);

        }
    }
}

在我的测试文件中,显示的错误是:
没有给出与“Walk.Walking(Array[])的必需形式参数“newWalk”相对应的参数。

我已经搜索了其他答案,可以看到如何将数组传递给函数,但似乎无法在测试文件中找到正确的方法。有人能帮忙吗?(如果这个问题非常基本,很抱歉,但我对C非常陌生)


谢谢!

而不是在Walking()方法中传递
数组[]
而传递
字符数组的实例。
像

当从NUnit测试传递它时,创建char数组的实例并将其作为参数传递给函数

老实说,我将传递直接计数,而不是传递整个数组,因为您只是在检查数组的长度

大概

   public bool Walking(int newWalkCount)
    {
        return newWalkCount == 10;
    }
在努尼特

    [Test]
    public void WalkMustOnlyLast10Minutes()
    {
        Walk walk = new Walk();
        var charArray = new char[] {'w', 's', 'e', 'e', 'n', 'n', 'e', 's', 'w', 'w'};

        //Passing length instead of entire array. Checking Assert.IsTrue()
        Assert.IsTrue(walk.Walking(charArray.Length));

    }

而不是在Walking()方法中传递
Array[]
,而是传递
char Array
的实例。 像

当从NUnit测试传递它时,创建char数组的实例并将其作为参数传递给函数

老实说,我将传递直接计数,而不是传递整个数组,因为您只是在检查数组的长度

大概

   public bool Walking(int newWalkCount)
    {
        return newWalkCount == 10;
    }
在努尼特

    [Test]
    public void WalkMustOnlyLast10Minutes()
    {
        Walk walk = new Walk();
        var charArray = new char[] {'w', 's', 'e', 'e', 'n', 'n', 'e', 's', 'w', 'w'};

        //Passing length instead of entire array. Checking Assert.IsTrue()
        Assert.IsTrue(walk.Walking(charArray.Length));

    }

将签名更改为
public string Walking(char[]newWalk)
,然后将签名更改为
public string Walking(char[]newWalk)
{'w','s','e','e','n','n','e','s','w','w'})
Array[]
是一个数组数组。您确定要这样做吗?非常感谢@prasadelkikikar-我按照建议更改为char数组,这很有效,但是我继续传递整个数组,因为稍后我要实现一个函数,其中包括检查数组中的元素是否匹配。谢谢:)@juemura7,我很高兴我的解决方案对YouTunks非常有效@Prasadelkikikar-我按照建议改成了字符数组,这很有效,但是我继续传递整个数组,因为以后我想实现一个函数,其中包括检查数组中的元素是否匹配。谢谢:)@juemura7,我很高兴我的解决方案有效为你