首页 > 解决方案 > NUnit - 没有找到合适的构造函数

问题描述

试图用 NUnit 3 测试一些东西。我有一个Country我正在尝试测试的对象。

[TestFixture]
public class Country : IComparable
{
    private String countryName;
    private float GDP;
    private float inflation;
    private float tradeBalance;
    private float HDIRanking;
    private List<String> tradePartners;
    private String displayPartnersInTable; //used to display trade partners in Data table, turns list into string with commas to be displayed nicely
    Country c;


    public Country(String countryName, float GDP, float inflation, float tradeBalance, float HDIRanking, List<String> tradePartners)
    {
        this.countryName = countryName;
        this.GDP = GDP;
        this.inflation = inflation;
        this.tradeBalance = tradeBalance;
        this.HDIRanking = HDIRanking;
        this.tradePartners = tradePartners;
    }

    [SetUp]
    public void Init()
    {
        List<String> l = new List<string>();
        l.Add("UK");
        Country c = new Country("Malta", (float)1.2, (float)2.3, (float)3.2, 1, new List<string>() { "UK" });
    }

    [Test]
    public void CountryTest()
    {
        Assert.AreEqual("Malta", c.countryName, "Wrong country");
    }

不断收到错误消息,提示找不到合适的构造函数。任何帮助,将不胜感激。

标签: c#nunit

解决方案


Cosmin Cretu 是正确的。在 NUnit v3 中,您需要为测试夹具“Country”添加默认的无参数构造函数。添加它。

public Country() { }

这种情况的可能重复,Nunit 测试给出结果 OneTimeSetUp:没有找到合适的构造函数


推荐阅读