实体框架6嘲笑包括dbset的方法 [英] Entity framework 6 mocking include method on dbset

查看:116
本文介绍了实体框架6嘲笑包括dbset的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已经开始搜索关于如何在EF6中模拟dbset的include方法的问题的解决方案。
问题在这里有很好的记载: -



http:/ /entityframework.codeplex.com/discussions/461731



不幸的是,似乎没有一个有效的解决方案。



有没有人找到解决方法?



我明白,我们不应该真的在嘲笑EF6上下文,而是项目领先已经坚持了。



提前感谢

解决方案

,这可能是一个faff!



在下面我设置了模拟上下文并设置并可以成功调用包含。我认为这个秘密调味汁是通过提供者,表达式和GetEnumerator来存储调用的,并且将stubbed上下文中的DbSet属性设置为stubbed集合,而不是将上下文存根返回。



GitHub上提供了一个可运行的示例

  [Test] 
public void CanUseIncludeWithMocks()
{
var child = new Child();
var parent = new Parent();
parent.Children.Add(child);

var parents = new List< Parent>
{

} .AsQueryable();

var children = new List< Child>
{
child
} .AsQueryable();

var mockContext = MockRepository.GenerateStub< TestContext>();

var mockParentSet = MockRepository.GenerateStub< IDbSet< Parent>>();
var mockChildSet = MockRepository.GenerateStub< IDbSet< Child>>();

mockParentSet.Stub(m => m.Provider).Return(parents.Provider);
mockParentSet.Stub(m => m.Expression).Return(parents.Expression);
mockParentSet.Stub(m => m.GetEnumerator())。Return(parents.GetEnumerator());

mockChildSet.Stub(m => m.Provider).Return(children.Provider);
mockChildSet.Stub(m => m.Expression).Return(children.Expression);
mockChildSet.Stub(m => m.GetEnumerator())。Return(children.GetEnumerator());

mockContext.Parents = mockParentSet;
mockContext.Children = mockChildSet;

mockContext.Parents.Should()。HaveCount(1);
mockContext.Children.Should()。HaveCount(1);

mockContext.Parents.First()。Children.FirstOrDefault()。Should()。NotBeNull();

var query = mockContext.Parents.Include(p => p.Children).Select(pc => pc);

query.Should()。NotBeNull()。And.HaveCount(1);
query.First()。Children.Should()。NotBeEmpty()。And.HaveCount(1);

}


Have been googling for a solution to the problem on how to mock the include method on dbset in EF6. The problem is well documented here :-

http://entityframework.codeplex.com/discussions/461731

Unfortunately though there does not seem to be a valid solution in there.

Has anyone found a workaround to this?

I do understand that we shouldn't really be mocking the EF6 context, but the project lead has insisted on it.

Thanks in advance.

解决方案

So, this is possible if a bit of a faff!

In the below I setup the mock context and sets and can call include successfully. I think that the secret sauce is in stubbing the calls through to Provider, Expression and GetEnumerator and in setting the DbSet properties on the stubbed context to the stubbed sets and not stubbing the context to returning them.

A runnable example is available on GitHub

    [Test]
    public void CanUseIncludeWithMocks()
    {
        var child = new Child();
        var parent = new Parent();
        parent.Children.Add(child);

        var parents = new List<Parent>
            {
                parent
            }.AsQueryable();

        var children = new List<Child>
            {
                child
            }.AsQueryable();

        var mockContext = MockRepository.GenerateStub<TestContext>();

        var mockParentSet = MockRepository.GenerateStub<IDbSet<Parent>>();
        var mockChildSet = MockRepository.GenerateStub<IDbSet<Child>>();

        mockParentSet.Stub(m => m.Provider).Return(parents.Provider);
        mockParentSet.Stub(m => m.Expression).Return(parents.Expression);
        mockParentSet.Stub(m => m.GetEnumerator()).Return(parents.GetEnumerator());

        mockChildSet.Stub(m => m.Provider).Return(children.Provider);
        mockChildSet.Stub(m => m.Expression).Return(children.Expression);
        mockChildSet.Stub(m => m.GetEnumerator()).Return(children.GetEnumerator());

        mockContext.Parents = mockParentSet;
        mockContext.Children = mockChildSet;

        mockContext.Parents.Should().HaveCount(1);
        mockContext.Children.Should().HaveCount(1);

        mockContext.Parents.First().Children.FirstOrDefault().Should().NotBeNull();

        var query = mockContext.Parents.Include(p=>p.Children).Select(pc => pc);

        query.Should().NotBeNull().And.HaveCount(1);
        query.First().Children.Should().NotBeEmpty().And.HaveCount(1);

    }

这篇关于实体框架6嘲笑包括dbset的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆