使用TestCaseSource导致空指针C#的NUnit [英] C# NUnit using TestCaseSource results in NullPointer

查看:244
本文介绍了使用TestCaseSource导致空指针C#的NUnit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类,这是它有一个测试和产生的TestCase我一个TestCaseSource一个的TestFixture。我的测试情况下,实际上包含两个实际结果与预期结果是被包裹在一个名为TestCaseEntity

I have the following class which is a TestFixture which has one test and a TestCaseSource that produces my TestCases. My test case actually contain both the actual result and the expected result which is wrapped in an Object called 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");
                    }
                }
            }
        }
    }
}

当我运行我的测试,我得到以下错误这是不幸的是不是非常有帮助!

When I run my Test, I get the following error which is unfortunately not very helpful!

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

这是我可能做错了?

推荐答案

您的foreach超过 allSalesDEDealIds ,但你似乎已经不在您的代码示例包含它。它可能是,这是 NullRefException 在你的代码?

You foreach over allSalesDEDealIds but you seem to have not included it in your code example. It might be that this is the NullRefExceptionin your code?

这篇关于使用TestCaseSource导致空指针C#的NUnit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆