RhinoMock - 一种方法中的多种期望 - 最佳实践

发布于 2024-10-10 04:20:25 字数 1725 浏览 5 评论 0原文

我想知道大家遇到这样的事情是怎么处理的。最好的做法是什么..

以免说我首先在演示者对象上有一个名为 Load 的方法,

该方法如下所示:

public void Load(ViewMode mode, int? id)
{
     if(mode == ViewMode.Modify)
          view.CurrentEntity = model.GetMyEntityById(id.GetValueOrDefault(0));
     else
          view.CurrentEntity = model.CreateNewEntity();
}

为了测试此代码,我使用 RhinoMock 进行了 2 个测试来设置期望

[TestMethod]
public void Load_ShouldCallModelToGetEntityIfViewModeEqualsModify()
{
     IView view = _mockery.StrictMock<IView>();
     IModel model = _mockery.StrictMock<IModel>();

     Entity e = new Entity()

     using(_mockery.Ordered())
     {
          Expec.Call(model.GetMyEntityById(3)).Return(e)
          view.CurrentEntity = e;
     }
     _mockery.ReplayAll()

    Presenter sut = new Presenter(view, model);

    sut.Load(ViewMode.Modify, 3);

    _mockery.VerifyAll();
}

然后我有另一个测试来测试另一个ViewMode.Add 的路径...

所以我的问题是,如果我将演示者上的 Load 方法更改为

public void Load(ViewMode mode, int? id)
{

     view.CollectionA = model.GetListOfA();  <------ADDED THIS 
     view.CollectionB = model.GetListOfB();  <------AND THIS

     if(mode == ViewMode.Modify)
          view.CurrentEntity = model.GetMyEntityById(id.GetValueOrDefault(0));
     else
          view.CurrentEntity = model.CreateNewEntity();
}

现在的样子,我必须在之前的所有测试中添加对两个 GetList 方法的期望,否则将导致 Expected 0 Actual 1另外,我还必须更改我的测试名称,因为在测试中我期望的不仅仅是调用模型通过其 id 获取实体。

那么有没有最佳实践呢?当我添加对某些模拟的调用时,更改我的所有测试是正确的处理方法,或者有一种方法可以期望仅对先前的测试进行一些调用并添加另一个测试,如下所示: [测试方法] 公共无效Load_ShouldCallModelToObtainAListOfA();

它不会使我的其他测试失败。

我知道这是一个模糊的问题,但如果有人理解我的问题,请告诉我你如何通过行为测试来处理这种情况。

非常感谢 违反

I would like to know how you guys handle something like this. Whats the best practice..

Lest say I have a Method called Load on a presenter object

at first the method looks like this :

public void Load(ViewMode mode, int? id)
{
     if(mode == ViewMode.Modify)
          view.CurrentEntity = model.GetMyEntityById(id.GetValueOrDefault(0));
     else
          view.CurrentEntity = model.CreateNewEntity();
}

To test this code I have 2 tests using RhinoMock to setup expectations

[TestMethod]
public void Load_ShouldCallModelToGetEntityIfViewModeEqualsModify()
{
     IView view = _mockery.StrictMock<IView>();
     IModel model = _mockery.StrictMock<IModel>();

     Entity e = new Entity()

     using(_mockery.Ordered())
     {
          Expec.Call(model.GetMyEntityById(3)).Return(e)
          view.CurrentEntity = e;
     }
     _mockery.ReplayAll()

    Presenter sut = new Presenter(view, model);

    sut.Load(ViewMode.Modify, 3);

    _mockery.VerifyAll();
}

And I then I have another test that test the other path for the ViewMode.Add...

So my question is if I changed the Load method on the presenter to looks like

public void Load(ViewMode mode, int? id)
{

     view.CollectionA = model.GetListOfA();  <------ADDED THIS 
     view.CollectionB = model.GetListOfB();  <------AND THIS

     if(mode == ViewMode.Modify)
          view.CurrentEntity = model.GetMyEntityById(id.GetValueOrDefault(0));
     else
          view.CurrentEntity = model.CreateNewEntity();
}

now I have to add the expectation for both GetList methods in all my previous test or it will result in Expected 0 Actual 1. Also I will have to change my test name because in the test I expect far more than just calling the model to get an entity by its id.

So is there a best pratice for that. Is changing all my tests is the correct way to do things when I add call to some mocks, or there is a way to expect just some calls for previous test and add another test that would be something like :
[TestMethod]
public void Load_ShouldCallModelToObtainAListOfA();

and it would not make my other tests to fail.

I know its a vague question but if somebody understand what I ask please tell me how you handle this kind of situation with behavior tests.

Thank you very much
Breach

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

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

发布评论

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

评论(1

倾`听者〃 2024-10-17 04:20:25

看起来您正在编写单元测试以确保调用某些方法来实现您想要的结果。不要这样做。它使您的测试非常脆弱并且使重构变得困难。

相反,只需测试 Load 方法的行为,而不是测试该行为的实现方式。从 Load 方法来看,它正在填充几个集合并设置 CurrentEntity 属性。因此,您的测试应该简单地确保在调用 Load 方法之后,集合已设置,并且 CurrentEntity 已设置为您期望的值。

存根模型以返回模型对象上调用的各种方法的一些预设响应,然后运行测试。验证预设值是否返回到您的视图中。

It looks like you're writing your unit tests to make sure certain methods are called to achieve the results you want. Don't do this. It makes your tests very brittle and makes refactoring difficult.

Instead, just test the behavior of your Load method, not how that behavior is implemented. From looking at your Load method, it's populating a couple of collections and setting the CurrentEntity property. So your test should simply make sure that after the Load method was called, the collections are set and that the CurrentEntity is set to the one you expect.

Stub out the model to return some canned responses for the various methods called on the model object and then run your test. Verify that the canned values come back in your view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文