在一个类中最小起订量只有一个方法 [英] moq only one method in a class

查看:177
本文介绍了在一个类中最小起订量只有一个方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用moq.dll
当我嘲笑一个类(所有IRepository接口)我用这个代码行

I'm using moq.dll When I mock a class(all the IRepository interface) i use this line code

   int state = 5;
   var rep = new Mock<IRepository>();
   rep.Setup(x => x.SaveState(state)).Returns(true);
   IRepository repository = rep.Object;



但在这种情况下,我在嘲笑repository类的所有功能。
然后,在类库中的所有方法都取代模拟DLL的方法设置

but in this case i mock all the function in repository class. Then all the methods in class repository are substituted with the methods setup of Mock dll

我想使用类存储库中定义的所有方法(真正的类)和模拟只有一个函数(即时存档)

I want use all the methods defined in class repository(the real class) and mock only one function(SaveState)

我怎样才能做到这一点? ?有可能。

How can I do this? Is possible?

推荐答案

您可以创建真实的版本库的一个实例,然后使用为<> ()来获得所需的接口,然后你就可以用设置覆盖,像这样的:

You can create an instance of the real repository, then use the As<>() to obtain the desired interface, which you can then override with the setup, like this:

var mockRep = new Mock<RealRepository>(ctorArg1, ctorArg2, ...)
                     .As<IRepository>();
mockRep.Setup(x => x.SaveState(state)).Returns(true);



然后 mockRep.Object 作为存储库的依赖被测类。
请注意,您只能在接口 *,或虚拟方法模拟的方法,用这种方法。

Then mockRep.Object as the repository dependency to the class under test. Note that you will only be able to Mock methods on the Interface*, or virtual methods, in this way.

更新的:*这可能不是在所有情况下工作,因为 .Setup 只能在虚拟方法,和C#接口实现是虚拟并密封 默认。并使用为()将防止部分仿制品的行为。

Update : *This might not work in all scenarios, since .Setup will only work on virtual methods, and C# interface implementations are "virtual" and sealed by default. And using As() will prevent the partial mock behaviour.

所以,现在看来, RealRepository 具体类将需要实施 IRepository ,以便虚拟方法为偏模拟成功,在这种情况下 CallBase 可用于电线的界面。

So it appears that the RealRepository concrete class will need to implement the IRepository interface with virtual methods in order for the partial mock to succeed, in which case CallBase can be used for the wire-up.

   public interface IRepo
   {
      string Foo();
      string Bar();
   }

   public class RealRepo : IRepo
   {
      public RealRepo(string p1, string p2) {Console.WriteLine("CTOR : {0} {1}", p1, p2); }
      // ** These need to be virtual in order for the partial mock Setups
      public virtual string Foo() { return "RealFoo"; }
      public virtual string Bar() {return "RealBar"; }
   }

   public class Sut
   {
      private readonly IRepo _repo;
      public Sut(IRepo repo) { _repo = repo; }

      public void DoFooBar()
      {
         Console.WriteLine(_repo.Foo());
         Console.WriteLine(_repo.Bar());
      }
   }


   [TestFixture]
   public class SomeFixture
   {
      [Test]
      public void SomeTest()
      {
        var mockRepo = new Mock<RealRepo>("1st Param", "2nd Param");
        // For the partially mocked methods
        mockRepo.Setup(mr => mr.Foo())
           .Returns("MockedFoo");
        // To wireup the concrete class.
        mockRepo.CallBase = true;
        var sut = new Sut(mockRepo.Object);
        sut.DoFooBar();
      }
   }

这篇关于在一个类中最小起订量只有一个方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆