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

时间:2012-07-19 09:53:53

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

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:3)

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

就像SQL LEFT JOIN

一样

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

而不是

LanguageCode = j2.LanguageCode

尝试做

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

答案 1 :(得分:0)

将模拟上下文传递给您的方法,LINQ将完成它的工作。然后验证返回的TranslationView对象列表。

以下是您的测试的样子:

List<Translations> translations = // create translations
List<Tokens> tokens = // create tokens
var context = MockRepository.GenerateStub<IMyContext>();            
context.Stub(c => c.Translations).Return(translations);
context.Stub(c => c.Tokens).Return(tokens);

var foo = new Foo(context);
var views = foo.GetTranslationView();
// verify returned views