模拟数据库上下文时如何使用DefaultIfEmpty?

Gaz*_*ter 5 c# linq unit-testing rhino-mocks

我编写了以下linq查询,该查询通过将数据连接在一起来创建一个新对象,如您所见:

 var translations = from t in context.Translations
                            join token in context.Tokens on t.Guid equals token.Guid
                            join t2 in context.Translations on new { t.Guid, LanguageCode = "fr" } equals new { t2.Guid, t2.LanguageCode} into j //TODO: fr needs to be replaced by the language of the translators account
                            from j2 in j.DefaultIfEmpty()
                           where t.LanguageCode == String.Empty
                           orderby t.Text
                           select new TranslationView
                                    {
                                        Guid = t.Guid,
                                        LanguageCode = j2.LanguageCode,
                                        SourceText = t.Text,
                                        Translation = j2.Text,
                                        IsNew = j2.Text == null,
                                        Notes = token.Notes,
                                        Required = token.Required,
                                        Type = (Token.TokenType)token.Type, 
                                        Location = (Token.LocationType)token.Location
                                    };
Run Code Online (Sandbox Code Playgroud)

问题是我现在正在尝试使用Rhino.Mocks编写单元测试,它返回错误 Object reference not set to an instance of an object.

所以我现在的问题是,有没有一种更好的方式可以编写此查询?在实际情况和单元测试情况下都可以使用的方法?

我尝试DefaultIfEmpty()在位中传递一个值,并使它适用于Mock,但随后主代码失败。

编辑 单元测试代码:

  [Test]
    public void Build_Translation_List_TwoItems_Context()
    {
        //Arrange: Setup context
        var context = setupContext();

        //Act: Pass the context through
        var result = TranslationHelpers.BuildTranslationList(context, 1);

        //Result
        result.TranslationList.Count.ShouldEqual(2);
        result.PagingInfo.TotalItems.ShouldEqual(2);
    }
Run Code Online (Sandbox Code Playgroud)

SetupContext方法:

  public static ITranslationContext setupContext()
    {
        var context = new Mock<ITranslationContext>();
        context.SetupProperty(x => x.Tokens, new UnitTestHelpers.FakeDbSet<Token>
                                                {
                                                    new Token
                                                        {
                                                            DateAdded = DateTime.Now,
                                                            Guid = Guid.Parse("f3099a43-e12d-4ea3-ba06-265fde807f03"),
                                                            LastUpdated = DateTime.Now,
                                                            Location = (short)0,
                                                            Type = (short)0,
                                                            LocationDescription = "Test 1",
                                                            Notes = "Testing 1",
                                                            Required = "Testing"
                                                        },

                                                    new Token
                                                        {
                                                            DateAdded = DateTime.Now,
                                                            Guid = Guid.Parse("7D6937D8-F7E1-4B92-934E-465683874B65"),
                                                            LastUpdated = DateTime.Now,
                                                            Location = (short)0,
                                                            Type = (short)0,
                                                            LocationDescription = "Test 3",
                                                            Notes = "Testing 3",
                                                            Required = "Testing"
                                                        },

                                                });
        context.SetupProperty(x => x.Translations, new UnitTestHelpers.FakeDbSet<Translation>
                                                    {
                                                        new Translation{Guid = Guid.Parse("f3099a43-e12d-4ea3-ba06-265fde807f03"), LanguageCode = String.Empty, Text = "Testing 1"},
                                                        new Translation{Guid = Guid.Parse("f3099a43-e12d-4ea3-ba06-265fde807f03"), LanguageCode = "fr", Text = ""},
                                                        new Translation{Guid = Guid.Parse("7D6937D8-F7E1-4B92-934E-465683874B65"), LanguageCode = String.Empty, Text = "Testing 3"},
                                                        new Translation{Guid = Guid.Parse("7D6937D8-F7E1-4B92-934E-465683874B67"), LanguageCode = "fr", Text = "Testing 4"}

                                                    });
        return context.Object;
    }
Run Code Online (Sandbox Code Playgroud)

任何帮助将非常感激。

Rap*_*aus 4

DefaultIfEmpty()不会创建“默认”j2。即使 j2 为空,它也只会获取数据。

就像 SQL 一样LEFT JOIN

所以你必须测试无效性以避免 NRE。

代替

LanguageCode = j2.LanguageCode
Run Code Online (Sandbox Code Playgroud)

试着做

LanguageCode =j2 != null ? j2.LanguageCode : string.Empty // or null
Run Code Online (Sandbox Code Playgroud)