How to mock everything?

半世苍凉 提交于 2019-12-08 18:27:48

问题


I understand how to mock interfaces or virtual method calls. But frameworks like TypeMock can mock everything in the framework. Which .NET mechanisms are used to provide such functions?


回答1:


Typemock Isolator for example uses the CLR profiler API to intercept .NET calls and change them during runtime:

Quote:

Typemock Isolator uses an aspect-oriented programming design that creates a mock aspect. Internally, it uses the .NET Framework profiler API to monitor an application's execution. When a method is loaded by the CLR, Typemock Isolator retrieves the IL and replaces it with instrumented IL code. Typemock Isolator does not change your original IL code, it simply inserts new code that calls the Typemock Isolator framework and returns mocked values.




回答2:


Mock Everything is one of the feature for Mock frameworks. Mock everything in short is to mock everything in a class, such as

  • mock dependent objects
  • non-virtual methods
  • sealed classes
  • static methods
  • static classes
  • non-public members
  • non-public types

JustMock & TypeMock both provide this feature.

You can actually look at source code of some open source mock frameworks such as Moq framework ,Easy Mock, Rhino Mocks etc.

Disclosure: Please note that link provided is of my own website



来源:https://stackoverflow.com/questions/3680629/how-to-mock-everything

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!