首先模拟DBSet,EF模型 [英] Mocking DBSet, EF Model First

查看:121
本文介绍了首先模拟DBSet,EF模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题中所述,我遵循模型优先"方法.因此,我的Model类是自动生成的.如果我想模拟DBContext派生的MyModelContainer,其中包含实体类的DBSets.阅读一些内容以进行单元测试,您需要将其更改为IDBSet.一个问题是,是否有可能做到这一点,特别是在我执行运行自定义工具"时会自动生成的类中.但是到目前为止,我已经对其进行了修改.

但是真正的问题是:当我尝试对MyModelContainer进行存根以返回从IDBSet生成的模拟时. Rhino模拟正在触发InvalidOperationException:无效的调用,上次使用的调用或未进行任何调用(请确保您正在调用virtual(C#)/Overridable(VB)方法."

这是我的单元测试代码.

MyModelContainer dbMock = MockRepository.GenerateMock<MyModelContainer>();
IDBSet<Models.MyEntity> entityMock = MockRepository.GenerateMock<IDBSet<Models.MyEntity>>()
dbMock.Stub( x=>x.MyEntities ).Return( entityMock );

最后一条语句正在触发异常.我尝试使用在此处指定的IDBSet<>的伪实现. ,但是没有运气!

我使用MVC 4,Rhino Mocks 3.6.任何帮助将不胜感激.

更新:

经过一些试验和研究,我找到了解决方法.我将代码更改为:

MyModelContainer dbMock = MockRepository.GenerateMock<MyModelContainer>();
IDBSet<Models.MyEntity> entityMock = MockRepository.GenerateMock<IDBSet<Models.MyEntity>>()
//dbMock.Stub( x=>x.MyEntities ).Return( entityMock );
dbMock.MyEntities = entityMock;

现在InvalidOperationException不见了. 测试失败仅是由于ExpectationViolationException应该是正常的.

对于自动生成的Model类,发现编辑DbContext's T4模板(扩展名为.tt)可以解决问题.感谢艾伦的博客

但是我想知道为什么以前的代码不起作用.有人吗?

解决方案

2种可能的原因在这里:

    MyModelContainer
  1. MyEntites属性不是虚拟的.
    在这种情况下,Rhino Mock根本无法使用此属性.然后dbMock.Stub(x=>x.MyEntities)将失败.

  2. MyEntites属性是虚拟的,但同时具有公共获取器和公共设置器.
    然后,不允许使用符号dbMock.Stub(x=>x.MyEntities).Return(entityMock).您可以查看说明,例如此处.

在两种情况下,正确的解决方案正是您所做的:使用dbMock.MyEntities = entityMock而不是dbMock.Stub(x=>x.MyEntities).Return(entityMock).

As said in the title, I follow Model First method. So my Model classes are Automatically generated. If I want mock the DBContext derived MyModelContainer which contain DBSets of entity classes. Read some where that in order to unit test, you need to change it to IDBSet. Whether its possible to do it especially in a class that gets auto generated when I do "Run Custom Tool" is one concern. But as of now I modified it.

But the real problem is: when I try to Stub MyModelContainer to return a mock generated from IDBSet. Rhino mock is firing an InvalidOperationException: "Invalid call, the last call has been used, or no call has been made(make sure that you are calling a virtual(C#)/Overridable(VB) method."

Here is my unit test code.

MyModelContainer dbMock = MockRepository.GenerateMock<MyModelContainer>();
IDBSet<Models.MyEntity> entityMock = MockRepository.GenerateMock<IDBSet<Models.MyEntity>>()
dbMock.Stub( x=>x.MyEntities ).Return( entityMock );

The last statement is triggering the exception. I tried using the fake implementation of IDBSet<> specified here, But no luck!

I use MVC 4, Rhino Mocks 3.6. Any help will be appreciated.

Update:

After some trials and research, I found a fix. I changed the code to:

MyModelContainer dbMock = MockRepository.GenerateMock<MyModelContainer>();
IDBSet<Models.MyEntity> entityMock = MockRepository.GenerateMock<IDBSet<Models.MyEntity>>()
//dbMock.Stub( x=>x.MyEntities ).Return( entityMock );
dbMock.MyEntities = entityMock;

Now the InvalidOperationException is gone. The test fails only due to ExpectationViolationException which should be normal.

As for auto generated Model class, it is found out that editing the DbContext's T4 template (.tt extension) will do the trick. Thanks to Alan's Blog

But I want to know why the previous code didn't work. Anyone?

解决方案

2 reasons are possible here:

  1. MyEntites property of MyModelContainer is not virtual.
    In that case Rhino Mock can't stub this property at all. Then dbMock.Stub(x=>x.MyEntities) will fail.

  2. MyEntites property is virtual, but has both public getter and public setter.
    Then notation dbMock.Stub(x=>x.MyEntities).Return(entityMock) is not allowed. You can see explanation e.g. here.

In both cases the right fix is exactly what you did: use dbMock.MyEntities = entityMock instead of dbMock.Stub(x=>x.MyEntities).Return(entityMock).

这篇关于首先模拟DBSet,EF模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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