【问题标题】:Mocking static functions using TypeMock使用 TypeMock 模拟静态函数
【发布时间】:2012-10-26 12:22:50
【问题描述】:

我正在尝试在我的项目中模拟静态函数。我无法使用 Rhynomocks 执行此操作,因此尝试使用 Typemock 模拟静态函数。

他们说可以使用 typemock 模拟静态函数,下面的文章中提供了相同的示例

http://www.typemock.com/basic-typemock-unit-testing

但是它似乎对我不起作用。以下是我的代码:

公共类 Class1Test
{
[孤立(设计 = DesignMode.Pragmatic)]
[测试]
公共无效函数()
{ Isolate.Fake.StaticMethods(Members.MustSpecifyReturnValues);

        Isolate.WhenCalled(() => LoggerFactory.Add(6, 4)).WillReturn(11);

        int value = LoggerFactory.Add(5, 6);
    }


}

----------------------------------- LoggerFactory.cs

公共类 LoggerFactory {

    public static  int Add(int intx, int inty)
    {
        return intx + inty;
    }

}

我得到的错误是:

* 在 InterfaceOnly 设计模式下,无法伪造非虚拟方法。使用 [Isolated(DesignMode.Pragmatic)] 来伪造它。在此处了解更多信息http://www.typemock.com/isolator-design-mode

提前致谢。

【问题讨论】:

    标签: mocking tdd typemock typemock-isolator


    【解决方案1】:

    你为什么要首先模拟?您的方法不需要模拟,因为它是无状态的 - 只需直接测试即可:

    [Test]
    public void Six_Plus_Five_Is_Eleven()
    {
        Assert.That(11, Is.EqualTo(LoggerFactory.Add(6, 5));
    }
    

    【讨论】:

      【解决方案2】:

      您的示例代码看起来不完整。我只是使用您的代码将稍微修改的复制品放在一起,它工作正常。具体来说,Isolate.Fake.StaticMethods 调用缺少您打算模拟的类型。

      using System;
      using NUnit.Framework;
      using TypeMock.ArrangeActAssert;
      
      namespace TypeMockDemo
      {
        public class LoggerFactory
        {
          public static int Add(int intx, int inty)
          {
            return intx + inty;
          }
        }
      
        // The question was missing the TestFixtureAttribute.
        [TestFixture]
        public class LoggerFactoryFixture
        {
          // You don't have to specify DesignMode.Pragmatic - that's the default.
          [Isolated(Design = DesignMode.Pragmatic)]
          [Test]
          public void Add_CanMockReturnValue()
          {
            // The LoggerFactory type needs to be specified here. This appeared
            // missing in the example from the question.
            Isolate.Fake.StaticMethods<LoggerFactory>(Members.MustSpecifyReturnValues);
      
            // The parameters in the Add call here are totally ignored.
            // It's best to put "dummy" values unless you are using
            // WithExactArguments.
            Isolate.WhenCalled(() => LoggerFactory.Add(0, 0)).WillReturn(11);
      
            // Note the parameters here. No WAY they add up to 11. That way
            // we know you're really getting the mock value.
            int value = LoggerFactory.Add(100, 200);
      
            // This will pass.
            Assert.AreEqual(11, value);
          }
        }
      }
      

      如果您可以将该代码粘贴到项目中(使用 Typemock 和 NUnit 引用)并且它不起作用,那么您可能无法正确执行测试,或者您的机器上可能存在错误配置。无论哪种情况,如果上述代码不起作用,您可能需要联系 Typemock 支持。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-09
        相关资源
        最近更新 更多