C#中测试数据集时出错

时间:2011-11-16 19:59:08

标签: c# asp.net sql-server

我编写了一个ASP.NET和C#项目,它使用数据集连接数据库。 我想在我为使用数据集的类创建单元测试时测试我的代码我得到以下错误:

Test method Test.CommodityRepositoryTest.GetCommoditiesTest threw exception: 
System.NullReferenceException: Object reference not set to an instance of an object.
at Terminal.Database.TransportCo.TransportCoTableAdapters.ConfigurationTableAdapter.InitConnection() in TransportCoDataSet.Designer.cs: line 12259
at Terminal.Database.TransportCo.TransportCoTableAdapters.ConfigurationTableAdapter.get_Connection() in TransportCoDataSet.Designer.cs: line 12144
at Terminal.Database.TransportCo.TransportCoTableAdapters.ConfigurationTableAdapter.InitAdapter() in TransportCoDataSet.Designer.cs: line 12227
at Terminal.Database.TransportCo.TransportCoTableAdapters.ConfigurationTableAdapter.get_Adapter() in TransportCoDataSet.Designer.cs: line 12133
at Terminal.Database.TransportCo.TransportCoTableAdapters.ConfigurationTableAdapter.GetData() in TransportCoDataSet.Designer.cs: line 12290

我必须提到我使用单例存储库,但它不会更改错误中的任何内容。

有没有人有任何想法?

这些是我的代码及其测试:

public List<Commodity> GetCommodities()
    {         
        var commoditySet=new CommodityTableAdapter();
        var commoditys = commoditySet.GetCommdity().ToList();
        if (commoditys.Count() == 0)
            throw new UserInterfaceException("هیچ کالایی در سیستم ثبت نشده است");
        var commoditiesName = new List<Commodity>();
        for (int i = 0; i < commoditys.Count(); i++)
            commoditiesName.Add(ConvertcommodityRowTocommodity(commoditys[i]));
        return commoditiesName;
    }





   [TestMethod()]
    public void GetCommoditiesTest()
    {
        var target = CommodityRepository.GetInstance(); // TODO: Initialize to an appropriate value
        var expected = new List<Commodity> {new Commodity() {HSCode = "323423", ID = 19}}; // TODO: Initialize to an appropriate value
        List<Commodity> actual = target.GetCommodities();
        Assert.AreEqual(expected[0].ID, actual[0].ID);            
        //Assert.Inconclusive("Verify the correctness of this test method.");
    }

1 个答案:

答案 0 :(得分:0)

您的输入不明确

var commoditys = commoditySet.GetCommdity().ToList();

使用显式类型

Object commoditys = commoditySet.GetCommdity().ToList();

然后您将收到适当的调试错误。