MoqAutoMocker 和原始构造函数参数

发布于 2024-10-16 17:18:35 字数 1092 浏览 5 评论 0原文

我是 StructureMap MoqAutoMocker 的狂热用户,然而,有时我们会遇到我们的“老朋友”。假设有一个类“Validator”,

public class Validator
{
   private string _connectionString;
   private IEventMachine _eventMachine;

   public Validator(string connectionString, IEventMachine eventMachine)
   {
      _connectionString = connectionString;
      _eventMachine = eventMachine;
   }
} 

上面的类并不重要,事实上,它可能会引起一些人的注意,我只是为了这篇文章而做的,因为我想不出更好的例子了鼻子。关键是它包含原始数据类型 (connectionString) 和接口 (eventMachine) 的混合 - 在单元测试期间,我通常会设定我的期望,例如

[TestMethod]
public void Validate_WhenCalled_PublishesEnterEvent()
{
    // Arrange
    var Instance = new MoqAutoMocker<Validator>();
    var eventMachineMock = Mock.Get(AutoMock.Get<IEventMachine>());

    // Act
    Instance.Validate();

    // Assert
    eventMachineMock.Verify(m => m.Publish( It.IsAny<string>(), Times.Once());        
}

:问题是: 上面的代码不起作用,因为 MoqAutoMocker 无法接受 connectionString 参数,因为它找不到它的接口(或任何其他与此相关的原语)。我的问题很简单:有没有办法告诉 MoqAutoMocker 这个值应该是什么?

预先感谢您的阅读。

I am an avid user of the StructureMap MoqAutoMocker, sometimes, however, we run into an "old friend" of ours. Assume a class "Validator"

public class Validator
{
   private string _connectionString;
   private IEventMachine _eventMachine;

   public Validator(string connectionString, IEventMachine eventMachine)
   {
      _connectionString = connectionString;
      _eventMachine = eventMachine;
   }
} 

The class above doesn't really matter, in fact, it will probably raise a few eyebrows, I'm just making it for this post, as I could not think of a better example off the tip of my nose. The point is that it contains a mix of primitive datatypes ( connectionString) and interfaces (eventMachine) - during unit testing, I typically set my expectations, such as:

[TestMethod]
public void Validate_WhenCalled_PublishesEnterEvent()
{
    // Arrange
    var Instance = new MoqAutoMocker<Validator>();
    var eventMachineMock = Mock.Get(AutoMock.Get<IEventMachine>());

    // Act
    Instance.Validate();

    // Assert
    eventMachineMock.Verify(m => m.Publish( It.IsAny<string>(), Times.Once());        
}

So, the question is:
The above will not work, because MoqAutoMocker fails to accept the connectionString argument, as it cannot find an interface for it (or any other primitive for that matter). My question is simply: Is there a way to tell MoqAutoMocker what this value should be?

In advance, thanks for reading.

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

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

发布评论

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

评论(1

我喜欢麦丽素 2024-10-23 17:18:35

不,我不相信有办法——这是 AutoMocker 的限制。

我们倾向于避免使用原始构造函数参数,而倾向于设置对象(请参阅我们如何处理应用程序配置)

No, I dont beleive there is a way - it is a limitation of the AutoMocker.

We tend to avoid primitive constructor params in favor of settings objects (see How we handle application configuration)

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