如何使用 RhinoMocks AssertWasCalled 具有三种不同类型的通用方法?

发布于 2024-10-03 08:31:59 字数 5686 浏览 7 评论 0原文

我正在尝试学习 Rhino Mocks AAA 语法,但在断言某个方法(具有任何参数值)被调用时遇到问题。我使用 Machine.Specifications 作为我的测试框架。

这个特定的方法是通用的,我想确保它被三种不同类型的调用三次。

repo.Save<T1>(anything), repo.Save<T2>(anything), and repo.Save<T3>(anything)

我对每种类型的函数进行了存根处理。但我得到了一个有趣的结果。 (下)

[Subject("Test")]
public class When_something_happens_with_constraint
{
    static IRepository repo;
    static TestController controller;
    static ActionResult result;

    Establish context = () =>
    {
        repo = MockRepository.GenerateMock<IRepository>();
        controller = new TestController(repo);
        repo.Stub(o => o.Save<Something>(Arg<Something>.Is.Anything));
        repo.Stub(o => o.Save<SomethingElse>(Arg<SomethingElse>.Is.Anything));
        repo.Stub(o => o.Save<AnotherOne>(Arg<AnotherOne>.Is.Anything));
    };

    //post data to a controller
    Because of = () => { result = controller.SaveAction(new SomethingModel() { Name = "test", Description = "test" }); };

    //controller constructs its own something using the data posted, then saves it. I want to make sure three calls were made.  
    It Should_save_something = () => repo.AssertWasCalled(o => o.Save<Somethign>(Arg<Something>.Is.Anything));
    It Should_save_something_else = () => repo.AssertWasCalled(o => o.Save<SomethingElse>(Arg<SomethingElse>.Is.Anything));
    It Should_save_another_one = () => repo.AssertWasCalled(o => o.Save<AnotherOne>(Arg<AnotherOne>.Is.Anything));
}

结果是两个异常和一个通过。

第一次调用抛出:

System.InvalidOperationException:没有设置要验证的期望,请确保操作中的方法调用是虚拟(C#)/可重写(VB.Net)方法调用

第二个抛出:

System.InvalidOperationException:记录时仅在模拟方法调用中使用 Arg。需要 1 个参数,2 个已定义。

第三个通过了……出于某种奇怪的原因。

我还尝试在我的设置中将GenerateMock() 与Expect 一起使用,以及将GenerateStub() 与Stub 一起使用。两者最终得到完全相同的结果。我一定做错了什么。

我正在使用: MachineSpec 0.3.0.0 和 RhinoMocks 3.6.0.0

有什么想法吗?

-----已修复----------

这是在 Lee 的帮助下的完整版本(工作版本)。我正在使用额外的(非 linq)层。我的实际问题是我的一项测试在离线真实代码中重新使用了错误的 lambda 变量。 它 Should_do_something = () => repo.AssertWasCalled(o=>repo.Save(data)); //bad lambda

所以这里有一个正确测试的示例以供参考。

using System;
using System.Linq;
using System.Collections.Generic;
using Machine.Specifications;
using Rhino.Mocks;

namespace OnlineTesting.Specifications
{
    public interface Repository
    {
        void Save<T>(T data);
        IQueryable<T> All<T>();
    }

    public interface Service
    {
        void SaveItem(Item data);
        void SaveAnotherItem(AnotherItem data);
        void SaveOtherItem(OtherItem data);
        List<Item> GetItems();
        List<AnotherItem> GetAnotherItems();
        List<OtherItem> GetOtherItems();
    }

    public class ConcreteService : Service
    {
        Repository repo;
        public ConcreteService(Repository repo)
        {
            this.repo = repo;
        }
        public void SaveItem(Item data)
        {
            repo.Save(data);
        }
        public void SaveAnotherItem(AnotherItem data)
        {
            repo.Save(data);
        }
        public void SaveOtherItem(OtherItem data)
        {
            repo.Save(data);
        }

        public List<Item> GetItems()
        {
            return repo.All<Item>().ToList();
        }
        public List<AnotherItem> GetAnotherItems()
        {
            return repo.All<AnotherItem>().ToList();
        }
        public List<OtherItem> GetOtherItems()
        {
            return repo.All<OtherItem>().ToList();
        }
    }

    public class Item
    {
        public int Id { get; set; }
    }
    public class OtherItem
    {
    }
    public class AnotherItem
    {
    }


    public class When_something_else_happens
    {
        Establish context = () =>
        {
            _repository = MockRepository.GenerateMock<Repository>();
            _service = new ConcreteService(_repository);
            _controller = new TestController(_service);

            _repository.Stub(o => o.Save<Item>(Arg<Item>.Is.Anything)).WhenCalled(
                new Action<MethodInvocation>((o) =>
                {
                    var data = o.Arguments.FirstOrDefault() as Item;
                    if (data != null && data.Id == 0)
                        data.Id++;
                }));
        };

        Because of = () => _controller.DoSomethingElse();

        It should_save_the_first_thing = () =>
             _repository.AssertWasCalled(repo => repo.Save(Arg<Item>.Is.Anything));

        It should_save_the_other_thing = () =>
             _repository.AssertWasCalled(repo => repo.Save(Arg<OtherItem>.Is.Anything));

        It should_save_the_last_thing = () =>
             _repository.AssertWasCalled(repo => repo.Save(Arg<AnotherItem>.Is.Anything));

        static Repository _repository;
        static TestController _controller;
        static Service _service;
    }

    public class TestController
    {
        readonly Service _service;

        public TestController(Service service)
        {
            _service = service;
        }

        public void DoSomethingElse()
        {
            _service.SaveItem(new Item());
            _service.SaveOtherItem(new OtherItem());
            _service.SaveAnotherItem(new AnotherItem());
        }
    }
}

I'm trying to learn Rhino Mocks AAA syntax, and I'm having trouble asserting a certain method (with any argument value) was called. I'm using Machine.Specifications as my testing framework.

This particular method is generic and I want to make sure it was called three times with three different types.

repo.Save<T1>(anything), repo.Save<T2>(anything), and repo.Save<T3>(anything)

I stubbed the function for each type. But I'm getting an interesting result. (below)

[Subject("Test")]
public class When_something_happens_with_constraint
{
    static IRepository repo;
    static TestController controller;
    static ActionResult result;

    Establish context = () =>
    {
        repo = MockRepository.GenerateMock<IRepository>();
        controller = new TestController(repo);
        repo.Stub(o => o.Save<Something>(Arg<Something>.Is.Anything));
        repo.Stub(o => o.Save<SomethingElse>(Arg<SomethingElse>.Is.Anything));
        repo.Stub(o => o.Save<AnotherOne>(Arg<AnotherOne>.Is.Anything));
    };

    //post data to a controller
    Because of = () => { result = controller.SaveAction(new SomethingModel() { Name = "test", Description = "test" }); };

    //controller constructs its own something using the data posted, then saves it. I want to make sure three calls were made.  
    It Should_save_something = () => repo.AssertWasCalled(o => o.Save<Somethign>(Arg<Something>.Is.Anything));
    It Should_save_something_else = () => repo.AssertWasCalled(o => o.Save<SomethingElse>(Arg<SomethingElse>.Is.Anything));
    It Should_save_another_one = () => repo.AssertWasCalled(o => o.Save<AnotherOne>(Arg<AnotherOne>.Is.Anything));
}

The result is two exceptions and a pass.

The first call throws:

System.InvalidOperationException: No expectations were setup to be verified, ensure that the method call in the action is a virtual (C#) / overridable (VB.Net) method call

The second one throws:

System.InvalidOperationException: Use Arg ONLY within a mock method call while recording. 1 arguments expected, 2 have been defined.

The third one passes...for some odd reason.

I've also tried using GenerateMock() with Expect in my setup as well as using GenerateStub() with Stub. Both ended up with the exact same result. I've gotta be doing something wrong.

I'm using:
MachineSpec 0.3.0.0 and RhinoMocks 3.6.0.0

Any ideas?

-----FIXED----------

Here's the full (working version) with Lee's help. I am using an extra (non-linq) layer. My actual problem was that one of my tests re-used the wrong lambda variable in the offline real code.
It Should_do_something = () => repo.AssertWasCalled(o=>repo.Save(data)); //bad lambda

So here's a sample of the correct test for reference.

using System;
using System.Linq;
using System.Collections.Generic;
using Machine.Specifications;
using Rhino.Mocks;

namespace OnlineTesting.Specifications
{
    public interface Repository
    {
        void Save<T>(T data);
        IQueryable<T> All<T>();
    }

    public interface Service
    {
        void SaveItem(Item data);
        void SaveAnotherItem(AnotherItem data);
        void SaveOtherItem(OtherItem data);
        List<Item> GetItems();
        List<AnotherItem> GetAnotherItems();
        List<OtherItem> GetOtherItems();
    }

    public class ConcreteService : Service
    {
        Repository repo;
        public ConcreteService(Repository repo)
        {
            this.repo = repo;
        }
        public void SaveItem(Item data)
        {
            repo.Save(data);
        }
        public void SaveAnotherItem(AnotherItem data)
        {
            repo.Save(data);
        }
        public void SaveOtherItem(OtherItem data)
        {
            repo.Save(data);
        }

        public List<Item> GetItems()
        {
            return repo.All<Item>().ToList();
        }
        public List<AnotherItem> GetAnotherItems()
        {
            return repo.All<AnotherItem>().ToList();
        }
        public List<OtherItem> GetOtherItems()
        {
            return repo.All<OtherItem>().ToList();
        }
    }

    public class Item
    {
        public int Id { get; set; }
    }
    public class OtherItem
    {
    }
    public class AnotherItem
    {
    }


    public class When_something_else_happens
    {
        Establish context = () =>
        {
            _repository = MockRepository.GenerateMock<Repository>();
            _service = new ConcreteService(_repository);
            _controller = new TestController(_service);

            _repository.Stub(o => o.Save<Item>(Arg<Item>.Is.Anything)).WhenCalled(
                new Action<MethodInvocation>((o) =>
                {
                    var data = o.Arguments.FirstOrDefault() as Item;
                    if (data != null && data.Id == 0)
                        data.Id++;
                }));
        };

        Because of = () => _controller.DoSomethingElse();

        It should_save_the_first_thing = () =>
             _repository.AssertWasCalled(repo => repo.Save(Arg<Item>.Is.Anything));

        It should_save_the_other_thing = () =>
             _repository.AssertWasCalled(repo => repo.Save(Arg<OtherItem>.Is.Anything));

        It should_save_the_last_thing = () =>
             _repository.AssertWasCalled(repo => repo.Save(Arg<AnotherItem>.Is.Anything));

        static Repository _repository;
        static TestController _controller;
        static Service _service;
    }

    public class TestController
    {
        readonly Service _service;

        public TestController(Service service)
        {
            _service = service;
        }

        public void DoSomethingElse()
        {
            _service.SaveItem(new Item());
            _service.SaveOtherItem(new OtherItem());
            _service.SaveAnotherItem(new AnotherItem());
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

葬シ愛 2024-10-10 08:31:59

每个人似乎都在掩盖的一点是,您不需要存根来执行“调用断言”。而且,按照您编写的方式,Arg.Is.Anything,它将忽略该类型。您没有验证任何通用约束。您想要使用 Arg.Is.TypeOf。有关更多详细信息,请查看文档

-----------------------------------------------
| Arg<T>.Is   |                               |
===============================================
| Anything()  | No constraints                |
-----------------------------------------------
| TypeOf<T>() | Argument is of a certain type |
-----------------------------------------------

您的固定代码片段仍然太复杂。您没有使用在“调用时”中保存的 data 对象。而且您不需要它来执行简单的“断言被调用”。

@leebrandt 的代码看起来正确且简单,没有引入自动模拟容器。

The point that everyone seems to be glossing over is that you do not need to stub in order to perform an "assert was called". And, the way you have it written, Arg<T>.Is.Anything, it'll ignore the type. You're not verifying any generic constraints. You want to use Arg<T>.Is.TypeOf<T>. Check the documentation for more details.

-----------------------------------------------
| Arg<T>.Is   |                               |
===============================================
| Anything()  | No constraints                |
-----------------------------------------------
| TypeOf<T>() | Argument is of a certain type |
-----------------------------------------------

Your Fixed code snippet is still too complicated. You aren't using the data object that you're saving in the "when called". And you don't need it to do a simple "assert was called".

@leebrandt's code looks as correct and simple as it gets, without introducing an automocking container.

万劫不复 2024-10-10 08:31:59

尝试一下。

[Subject("Test")]
public class When_something_happens_with_constraint
{
    static IRepository repo;
    static TestController controller;
    static ActionResult result;

    Establish context = () =>
    {
        repo = MockRepository.GenerateMock<IRepository>();
        controller = new TestController(repo);
    };

    //post data to a controller
    Because of = () => result = controller.SaveAction(new SomethingModel() { Name = "test", Description = "test" });

    //controller constructs its own something using the data posted, then saves it. I want to make sure three calls were made.  
    It Should_save_something = () => repo.AssertWasCalled(o => o.Save(Arg<Something>.Is.Anything));
    It Should_save_something_else = () => repo.AssertWasCalled(o => o.Save(Arg<SomethingElse>.Is.Anything));
    It Should_save_another_one = () => repo.AssertWasCalled(o => o.Save(Arg<AnotherOne>.Is.Anything));
}

Give this a try.

[Subject("Test")]
public class When_something_happens_with_constraint
{
    static IRepository repo;
    static TestController controller;
    static ActionResult result;

    Establish context = () =>
    {
        repo = MockRepository.GenerateMock<IRepository>();
        controller = new TestController(repo);
    };

    //post data to a controller
    Because of = () => result = controller.SaveAction(new SomethingModel() { Name = "test", Description = "test" });

    //controller constructs its own something using the data posted, then saves it. I want to make sure three calls were made.  
    It Should_save_something = () => repo.AssertWasCalled(o => o.Save(Arg<Something>.Is.Anything));
    It Should_save_something_else = () => repo.AssertWasCalled(o => o.Save(Arg<SomethingElse>.Is.Anything));
    It Should_save_another_one = () => repo.AssertWasCalled(o => o.Save(Arg<AnotherOne>.Is.Anything));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文