c# – 验证传递给Mock的参数的正确方法是按预期设置的

前端之家收集整理的这篇文章主要介绍了c# – 验证传递给Mock的参数的正确方法是按预期设置的前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如果稍后验证方法调用,在回调中执行断言是否可以接受?这是确保我的mock获取传递给它的预期参数的首选方法,还是应该在我的回调中设置局部变量并在该实例上执行断言?

我有一种情况,我在Presenter类中有一些逻辑,它根据输入派生值并将它们传递给Creator类.为了测试Presenter类中的逻辑,我想验证在调用Creator时是否遵守了正确的派生值.我想出了下面的例子,但是我不确定我是否喜欢这种方法

  1. [TestFixture]
  2. public class WidgetCreatorPresenterTester
  3. {
  4. [Test]
  5. public void Properly_Generates_DerivedName()
  6. {
  7. var widgetCreator = new Mock<IWidgetCreator>();
  8. widgetCreator.Setup(a => a.Create(It.IsAny<Widget>()))
  9. .Callback((Widget widget) =>
  10. Assert.AreEqual("Derived.Name",widget.DerivedName));
  11.  
  12. var presenter = new WidgetCreatorPresenter(widgetCreator.Object);
  13. presenter.Save("Name");
  14.  
  15. widgetCreator.Verify(a => a.Create(It.IsAny<Widget>()),Times.Once());
  16. }
  17. }

我很担心,因为最后没有Verify调用,不能保证调用回调中的断言.另一种方法是在回调中设置局部变量:

  1. [Test]
  2. public void Properly_Generates_DerivedName()
  3. {
  4. var widgetCreator = new Mock<IWidgetCreator>();
  5. Widget localWidget = null;
  6. widgetCreator.Setup(a => a.Create(It.IsAny<Widget>()))
  7. .Callback((Widget widget) => localWidget = widget);
  8.  
  9. var presenter = new WidgetCreatorPresenter(widgetCreator.Object);
  10. presenter.Save("Name");
  11.  
  12. widgetCreator.Verify(a => a.Create(It.IsAny<Widget>()),Times.Once());
  13. Assert.IsNotNull(localWidget);
  14. Assert.AreEqual("Derived.Name",localWidget.DerivedName);
  15. }

我觉得这种方法不易出错,因为它更明确,并且更容易看到将调用Assert语句.一种方法比另一种更好吗?有没有更简单的方法来测试传递给我缺少的模拟的输入参数?

如果它有用,这是此示例的其余代码

  1. public class Widget
  2. {
  3. public string Name { get; set; }
  4. public string DerivedName { get; set; }
  5. }
  6.  
  7. public class WidgetCreatorPresenter
  8. {
  9. private readonly IWidgetCreator _creator;
  10.  
  11. public WidgetCreatorPresenter(IWidgetCreator creator)
  12. {
  13. _creator = creator;
  14. }
  15.  
  16. public void Save(string name)
  17. {
  18. _creator.Create(
  19. new Widget { Name = name,DerivedName = GetDerivedName(name) });
  20. }
  21.  
  22. //This is the method I want to test
  23. private static string GetDerivedName(string name)
  24. {
  25. return string.Format("Derived.{0}",name);
  26. }
  27. }
  28.  
  29. public interface IWidgetCreator
  30. {
  31. void Create(Widget widget);
  32. }

编辑
我更新了代码,使我在问题中概述的第二种方法更容易使用.我将Setup / Verify中使用的表达式的创建拉成了一个单独的变量,所以我只需要定义一次.我觉得这种方法是我最熟悉的,它很容易设置并且失败并且有很好的错误信息.

  1. [Test]
  2. public void Properly_Generates_DerivedName()
  3. {
  4. var widgetCreator = new Mock<IWidgetCreator>();
  5. Widget localWidget = null;
  6.  
  7. Expression<Action<IWidgetCreator>> expressionCreate =
  8. (w => w.Create(It.IsAny<Widget>()));
  9. widgetCreator.Setup(expressionCreate)
  10. .Callback((Widget widget) => localWidget = widget);
  11.  
  12. var presenter = new WidgetCreatorPresenter(widgetCreator.Object);
  13. presenter.Save("Name");
  14.  
  15. widgetCreator.Verify(expressionCreate,localWidget.DerivedName);
  16. }
@H_404_22@解决方法
由于代码的结构方式,你有点被迫在一个单元测试中测试两件事.您正在测试A)您的演示者正在调用注入的WidgetCreator的创建方法,并且B)在新的Widget上设置了正确的名称.如果可能的话,如果你能以某种方式将这两件事分成两个单独的测试,那就更好了,但在这种情况下,我真的没有办法做到这一点.

鉴于这一切,我认为第二种方法更清洁.它更明确地表达了你所期待的东西,如果它失败了,它就会完全理解为什么以及失败的原因.

猜你在找的C#相关文章