【问题标题】:Why do I get a NullReferenceException in this MSpec test?为什么在此 MSpec 测试中会出现 NullReferenceException?
【发布时间】:2016-10-05 12:16:19
【问题描述】:

所以我安装了这些 nuget 包:

在这些参考文献中达到高潮:

我使用 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 直截了当,我得到空引用异常。调试时无法单步执行该方法,并且所有规范类字段均为空.....

我不觉得我错过了什么,但如果你不介意看看我在这里做错了什么,那就太好了。我正在使用最新的 VS2015、NCrunch 等...

【问题讨论】:

    标签: c# mspec ncrunch


    【解决方案1】:

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

    private Establish context => () =>
    {
        pattern = "path";
    };
    
    private Because of => () => result = Subject.GetGlobs(pattern);
    

    我已经输入了=&gt; 而不是=....

    // ----------------------\/-------
    private Establish context = () =>
    {
        pattern = "path";
    };
    
    // ----------------\/------------    
    private Because of = () => result = Subject.GetGlobs(pattern);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-27
      相关资源
      最近更新 更多