简体   繁体   English

为什么在此MSpec测试中得到NullReferenceException?

[英]Why do I get a NullReferenceException in this MSpec test?

So I have these nuget packages installed: 所以我安装了这些nuget软件包:

努吉特

Culminating in these references: 在这些参考文献中总结如下:

参考文献

I use NCrunch. 我使用NCrunch。 I have this spec: 我有这个规格:

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");
            };
    }
}

For this class: 对于本课程:

namespace GlobPatternMatching
{
    using System;

    public class GlobMatching
    {
        public string[] GetGlobs(string pattern)
        {
            return pattern.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
        }
    }
}

TDD'ing straight up, I get null reference exception. TDD直线上升,我得到空引用异常。 When I debug I can't step through the method, and all the spec class fields are null..... 当我调试时,我无法单步执行该方法,并且所有spec类字段均为null .....

I don't feel like I'm missing anything, but if you don't mind having a look and figuring what I've done wrong here that would be good. 我不觉得我会错过任何东西,但是如果您不介意看一下并弄清楚我在哪里做错了,那会很好。 I'm using latest VS2015, NCrunch etc... 我正在使用最新的VS2015,NCrunch等...

You wouldn't believe what the issue was... 您不会相信问题出在哪里...

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

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

I've put => instead of = .... 我放=>而不是= ...。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM