模拟 IObjectSet与犀牛模拟

发布于 2024-10-20 17:07:47 字数 877 浏览 1 评论 0原文

有没有办法使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最单纯的乌龟 2024-10-27 17:07:47

我知道我参加这个聚会迟到了,但这里有一个我之前使用过的 IObjectSet 的简单实现。我忘记从哪里得到的:

public class MockObjectSet<T> : IObjectSet<T> where T : class {
        readonly List<T> _container = new List<T>();

        public void AddObject(T entity) {
            _container.Add(entity);
        }

        public void Attach(T entity) {
            _container.Add(entity);
        }

        public void DeleteObject(T entity) {
            _container.Remove(entity);
        }

        public void Detach(T entity) {
            _container.Remove(entity);
        }

        public IEnumerator<T> GetEnumerator() {
            return _container.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator() {
            return _container.GetEnumerator();
        }

        public Type ElementType {
            get { return typeof(T); }
        }

        public System.Linq.Expressions.Expression Expression {
            get { return _container.AsQueryable<T>().Expression; }
        }

        public IQueryProvider Provider {
            get { return _container.AsQueryable<T>().Provider; }
        }
    }

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:

public class MockObjectSet<T> : IObjectSet<T> where T : class {
        readonly List<T> _container = new List<T>();

        public void AddObject(T entity) {
            _container.Add(entity);
        }

        public void Attach(T entity) {
            _container.Add(entity);
        }

        public void DeleteObject(T entity) {
            _container.Remove(entity);
        }

        public void Detach(T entity) {
            _container.Remove(entity);
        }

        public IEnumerator<T> GetEnumerator() {
            return _container.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator() {
            return _container.GetEnumerator();
        }

        public Type ElementType {
            get { return typeof(T); }
        }

        public System.Linq.Expressions.Expression Expression {
            get { return _container.AsQueryable<T>().Expression; }
        }

        public IQueryProvider Provider {
            get { return _container.AsQueryable<T>().Provider; }
        }
    }
你的笑 2024-10-27 17:07:47

我会使用一个具体的实例,或者一个简单的假实例。该接口具有少量方法,并且实现看起来很简单。模拟该接口只会增加不必要的复杂性。

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.

浸婚纱 2024-10-27 17:07:47

由于您正在模拟一个接口,因此没有实际的代码。只需为您的接口设置一个存根,然后存根 Projects 属性即可返回您想要的内容(我假设 Projects 是一个属性,但您没有包含 Project 类的定义)。

像这样的东西应该有效:

var stubSet = MockRepository.GenerateStub<IObjectSet<Project>>();
stubSet.Stub(s => s.Projects).Return(new[]
                                                {
                                                    new Project {Categories = null},
                                                    new Project {Categories = "abc"}
                                                });

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:

var stubSet = MockRepository.GenerateStub<IObjectSet<Project>>();
stubSet.Stub(s => s.Projects).Return(new[]
                                                {
                                                    new Project {Categories = null},
                                                    new Project {Categories = "abc"}
                                                });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文