模拟 IObjectSet与犀牛模拟
有没有办法使用Rhino Mocks 为IObjectSet
生成存根?
之后的内容类似于以下代码:
var context = MockRepository.GenerateMock <IContext>();
//generate stub
var mockProjectObjectSet = MockRepository.GenerateStub<IObjectSet<Project>>();
TestObjectSets.GenerateFakeProjectList(mockProjectObjectSet);
context.Expect(p => p.Projects).Return(mockProjectObjectSet);
var projectRepository = new ProjectRepository(context);
在 GenerateFakeProjectList
静态辅助方法中,我只是创建指定类型的对象,并通过 AddObject
将它们添加到存根中IObjectSet
上的 > 方法:
public static IObjectSet<Project> GenerateFakeProjectList(IObjectSet<Project> projectsObjectSet)
{
projectsObjectSet.AddObject(new Project()
{
Categories = null,
DateCreated = DateTime.Now.AddDays(-10),
.......
Is there a way of using Rhino Mocks to generate Stub for an IObjectSet<T>
?
What is am after is something like the following code:
var context = MockRepository.GenerateMock <IContext>();
//generate stub
var mockProjectObjectSet = MockRepository.GenerateStub<IObjectSet<Project>>();
TestObjectSets.GenerateFakeProjectList(mockProjectObjectSet);
context.Expect(p => p.Projects).Return(mockProjectObjectSet);
var projectRepository = new ProjectRepository(context);
In the GenerateFakeProjectList
static helper method, I am simply creating the objects of the type specified and adding them to the stub via the AddObject
method on the IObjectSet
:
public static IObjectSet<Project> GenerateFakeProjectList(IObjectSet<Project> projectsObjectSet)
{
projectsObjectSet.AddObject(new Project()
{
Categories = null,
DateCreated = DateTime.Now.AddDays(-10),
.......
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我知道我参加这个聚会迟到了,但这里有一个我之前使用过的
IObjectSet
的简单实现。我忘记从哪里得到的:I know I'm late to this party, but here's a simple implementation of
IObjectSet<T>
that I've used before. I forget where I got it from:我会使用一个具体的实例,或者一个简单的假实例。该接口具有少量方法,并且实现看起来很简单。模拟该接口只会增加不必要的复杂性。
I'd use a concrete instance, or a simple fake. That interface has a small number of methods and the implementation appears trivial. Mocking that interface just adds unnecessary complexity.
由于您正在模拟一个接口,因此没有实际的代码。只需为您的接口设置一个存根,然后存根 Projects 属性即可返回您想要的内容(我假设
Projects
是一个属性,但您没有包含 Project 类的定义)。像这样的东西应该有效:
Since you're mocking an interface, there's no actual code to all. Just set up a stub for your interface and then stub out the Projects property to return what you want (I assume
Projects
is a property, but you didn't include a definition of the Project class).Something like this should work: