Troskyvs
Troskyvs

Reputation: 8047

C# NUnit reports weird error: "No suitable constructor was found"

I'm using VS2019 and created a NUnit test project with a .NET Core template, then I added this code:

using NUnit.Framework;

namespace xxx
{
    class Immutable
    {
        public Immutable(string _cur, string _addr)
        {
            Currency = _cur;
            Address = _addr;
        }

        public string Currency { get; }
        public string Address { get; }

        [Test]
        public static void Test() // reports this line has problem?
        {
            var m = new Immutable("usd", "us");
            string s = m.Currency;
            Assert.AreEqual("usd", s);
        }
    }
}

Build ok, but when I run it, test explorer reports this:

Test
Source: xxx.cs line 17
Duration: < 1 ms

Message: 
    OneTimeSetUp: No suitable constructor was found

I don't quite get what the problem is, how to fix it?

Upvotes: 1

Views: 453

Answers (1)

Keven Li
Keven Li

Reputation: 634

If your test class have parameterized constructor, you need TestFixture attribute with parameters to construct it.

Try this:

[TestFixuture("usd", "us")]
class Immutable
{
    ...
}

See the document

And strongly recommend you separate a test class for testing.

[TestFixture]
public class ImmutableTest
{
    [Test]
    public void Test()
    {
        var m = new Immutable("usd", "us");
        string s = m.Currency;
        Assert.AreEqual("usd", s);
    }
}

Upvotes: 3

Related Questions