How to mock IDbSet with Rhino Mocks

允我心安 提交于 2019-12-04 04:05:52

问题


I can't get this working at all. I've got this code in my test:

MockRepository repository = new MockRepository();
IDbSet<SystemUser> userSet = repository.StrictMock<IDbSet<SystemUser>>();
Expect.Call(userSet.Any(u => u.Id == "UserName")).Return(true);
// More code follows

But it bombs out on the StrictMock line with the error:

System.TypeLoadException: Method 'Create' on type 'IDbSet`1Proxy1862178487664986a7bd03ad3b5c6f2c' from assembly 'DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=a621a9e7e5c32e69' tried to implicitly implement an interface method with weaker type parameter constraints

Any ideas what this could be?

Thanks!


回答1:


You have encountered a known issue that Rhino.Mocks (version 3.6.0.0) has with generic method constraints.

The 'Create' method:

TDerivedEntity Create<TDerivedEntity>() where TDerivedEntity : class, TEntity

is failing to be generated correctly because of the constraints where TDerivedEntity : class, TEntity.

There is more information regarding the bug here : Can't get RhinoMocks to emit a mock that follows the generic type restriction rules

Unfortunately, it looks like we are going to have to wait for the next version of Rhino.Mocks to address the issue.




回答2:


For those who are still looking for a solution for this problem. rhino mocks 3.6.1 and later solves this issue.



来源:https://stackoverflow.com/questions/7360031/how-to-mock-idbset-with-rhino-mocks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!