質問

I am trying to mock static functions in my project. I am unable to do this using Rhynomocks hence trying to mock static functions using Typemock.

They say it is possbile to mock static function using typemock and the same example is provided in below article

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

however it doesn't seem to work for me. Below is my code:

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

public class LoggerFactory {

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

}

Error what I get is:

* Faking non-virtual methods is not possible in InterfaceOnly design mode. Use [Isolated(DesignMode.Pragmatic)] to fake this. Learn more here http://www.typemock.com/isolator-design-mode

Thanks in advance.

役に立ちましたか?

解決

Your example code looks incomplete. I just put a slightly modified reproduction together using your code and it works fine. In specific, the Isolate.Fake.StaticMethods call is missing the type you intend on mocking.

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);
    }
  }
}

If you can paste that code into a project (with Typemock and NUnit references) and it doesn't work, then you may be having troubles executing the tests properly, or you may have a misconfiguration on your machine. In either case, you will probably want to contact Typemock Support if the above code doesn't work.

他のヒント

Why are you trying to Mock in the first place? Your method doesn't need mocking, because it is stateless - just test it directly:

[Test]
public void Six_Plus_Five_Is_Eleven()
{
    Assert.That(11, Is.EqualTo(LoggerFactory.Add(6, 5));
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top