使用TestCaseSource的C#NUnit导致NullPointer

时间:2015-02-20 08:08:53

标签: c# nunit

我有以下类,它是一个TestFixture,它有一个test和一个产生我的TestCases的TestCaseSource。我的测试用例实际上包含实际结果和包含在名为TestCaseEntity的对象中的预期结果

namespace SomeNamespace
{
    [TestFixture]
    public class MyTest
    {
        static Client client;
        static List<string> userIds = new List<string>();

        [TestFixtureSetUp]
        public void Init()
        {
            // Set up a client
            client = new Client("server address"); // a HTTP client

            // populate a List of userIds from a local text test file
            userIds.Add(GetAllIdsFromTxtFile());
        }

        [Test, TestCaseSource(typeof(MyTestCaseEntityFactory), "MyTestCases")]
        public void CheckExpectations(TestCaseEntity testCaseEntity)
        {
            if (!testCaseEntity.IsIdentical)
            {
                Log.Error("Log some shit");
                Assert.Fail("fail assertions");
            }
        }

        public class MyTestCaseEntityFactory
        {
            public static IEnumerable<TestCaseEntity> MyTestCases
            {
                get
                {
                    foreach (string id in userIds)
                    {
                        // use the client and get the results, construct the new TestCaseEntity(...) and return;
                        yield return new TestCaseEntity("actualValue", "expectedValue resulting from the server call");
                    }
                }
            }
        }
    }
}

当我运行我的测试时,我收到以下错误,遗憾的是这不是很有帮助!

System.NullReferenceException : Object reference not set to an instance of an object.

有关我可能做错的任何建议吗?

1 个答案:

答案 0 :(得分:0)

您预告allSalesDEDealIds,但您似乎没有将其包含在您的代码示例中。这可能是您代码中的NullRefException