但是我想知道为什么以前的代码不起作用.有人吗? 解决方案 2种可能的原因在这里:MyModelContainer的 MyEntites属性不是虚拟的.在这种情况下,Rhino Mock根本无法使用此属性.然后dbMock.Stub(x=>x.MyEntities)将失败. 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 BlogBut I want to know why the previous code didn't work. Anyone? 解决方案 2 reasons are possible here: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.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模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-29 15:58