1

プロジェクトで静的関数をモックしようとしています。Rhynomocks を使用してこれを行うことができないため、Typemock を使用して静的関数をモックしようとしています。

彼らは、typemock を使用して静的関数をモックすることは可能であると述べており、同じ例が以下の記事で提供されています。

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

しかし、それは私にはうまくいかないようです。以下は私のコードです:

public class Class1Test
{
[Isolated(Design = DesignMode.Pragmatic)]
[Test]
public void function()
{ 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

前もって感謝します。

4

2 に答える 2

1

そもそもなぜモックしようとしているのですか?メソッドはステートレスであるため、モックを作成する必要はありません。直接テストするだけです。

[Test]
public void Six_Plus_Five_Is_Eleven()
{
    Assert.That(11, Is.EqualTo(LoggerFactory.Add(6, 5));
}
于 2012-10-27T20:25:57.510 に答える
0

あなたのコード例は不完全に見えます。あなたのコードを使用して、わずかに変更された複製をまとめただけで、正常に動作します。具体的には、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 サポートに連絡することをお勧めします。

于 2012-10-31T21:48:04.490 に答える