为什么我在这个MSpec测试中得到NullReferenceException?

时间:2016-10-05 12:16:20

标签: c# mspec ncrunch

所以我安装了这些nuget包:

Nuget

最终引用这些参考文献:

References

我使用NCrunch。我有这个规范:

namespace GlobPatternMatching.Tests
{
    using FluentAssertions;

    using Machine.Fakes;
    using Machine.Specifications;

    [Subject(typeof(GlobMatching))]
    public class When_Given_Literal : WithSubject<GlobMatching>
    {
        private static string pattern = "path";

        private static string[] result;

        private Establish context => () =>
            {
                pattern = "path";
            };

        private Because of => () => result = Subject.GetGlobs(pattern);

        private It should_return_path_in_the_array = () =>
            {
                result[0].Should().Be("path");
            };
    }
}

对于这堂课:

namespace GlobPatternMatching
{
    using System;

    public class GlobMatching
    {
        public string[] GetGlobs(string pattern)
        {
            return pattern.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
        }
    }
}
直接上传TDD,我得到空引用异常。当我调试时,我无法逐步执行该方法,并且所有spec类字段都为null .....

我不觉得自己错过了任何东西,但是如果你不介意看看我在这里做错了什么就行不通。我正在使用最新的VS2015,NCrunch等......

1 个答案:

答案 0 :(得分:2)

你不会相信这个问题是什么......

private Establish context => () =>
{
    pattern = "path";
};

private Because of => () => result = Subject.GetGlobs(pattern);

我已将=>代替= ....

// ----------------------\/-------
private Establish context = () =>
{
    pattern = "path";
};

// ----------------\/------------    
private Because of = () => result = Subject.GetGlobs(pattern);