How do mock frameworks work?

前端 未结 4 934
再見小時候
再見小時候 2021-01-04 18:18

If I was to write a mocking library, how would this work (in other words, how do \"they work?)?

One of the things which I wonder is that you are always setting expec

4条回答
  •  悲&欢浪女
    2021-01-04 18:58

    One way to look at how mock system work is just look at times when you need an object but you don't want to use the real class but instead want it to give you some specific kind of data that it wouldn't (or won't do so reliably). So if you see:

    Assert.IsTrue(myLogic.IsNoon(time))
    

    you can see how the assert would want the time object to always be noon. . . well you can't do that with a real object reliably. So you need a stand-in. You can make a fake class just for the test, but that's sort of heavy. Mock frameworks are a shortcut.

提交回复
热议问题