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

标签 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
                                    };

问题是我现在正尝试使用 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);
    }

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

如有任何帮助,我们将不胜感激。

最佳答案

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

这就像一个 SQL LEFT JOIN

因此您必须测试无效性以避免 NRE。

代替

LanguageCode = j2.LanguageCode

努力去做

LanguageCode =j2 != null ? j2.LanguageCode : string.Empty // or null

关于c# - 模拟数据库上下文时如何使用 DefaultIfEmpty?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11558244/

相关文章:

c# - Linq Union 的问题行为?

c# - 使用参数中的字段名称对 IList 进行排序

C# LINQ 查询

c++ - 是否可以在 boost::test 上使用自动注册的 BOOST_PARAM_TEST_CASE?

.net - 用于单元测试的单个实现者接口(interface)是反模式吗?

python - 测试 Nose 中是否返回正确的对象(Learn Python the Hard Way Ex 49)

c# - 在两个字符串字段上实现 IComparable 接口(interface)

c# - If - else 没有 else 的语句

c# - 如何确定var的大小?

c# - 为什么某些 Silverlight 对象不能是 Nullable<> 类型?