The difference between gaskets and plugs

Can someone clearly tell me what is the main difference between shim and a stub during unit testing?

I know about mocking and false objects, and I read about gaskets and stubs, but it is still unclear in what context I should use a gasket or stub.

+7
unit-testing
source share
4 answers

Let me cite an article by Martin Fowler Mocks Are not Stubs :

Stubs provide canned answers to calls made during the test, usually without reacting at all to anything outside of what is programmed for the test. Travelers can also record information about calls, such as the email address of the gateway that remembers the messages he sent, or maybe just how many messages he sent. "

Mocks - [...] objects pre-programmed with expectations that form the specification of the calls they should receive.

Thus, mocks can directly check if the expected violation is violated. Billets do not.

Gaskets (or Moles) differ from both of them in that they can be used to replace hard-coded dependencies, such as static methods. You should avoid this IMO and prefer refactoring, which makes these dependencies replaceable. See this topic for further discussion, especially Jim Cooper's answer.

+7
source share

From what I understood, the difference is where the mocked code is located or what part of the code you are mocking (ie, "where" gives the difference). Therefore, I would say that:

You call Stub the part of your code that you are mocking while it will call Shim , if you are mocking an external call.

  • "your code" is code for testing
  • an β€œexternal call” is the dependency your code depends on.

I found this link good: My two cents on fakes, stubs, poppies and esp boards are part of the "General rule is to use stubs for internal calls and gaskets for external assemblies."

+2
source share

As a general guide, use stubs for calls in your Visual Studio solution and spacers for calls to other reference assemblies. This is because in your own solution, it is good practice to decouple the components by defining the interfaces as required. But external assemblies, such as System.dll, usually do not contain separate interface definitions, so you should use paddings .

enter image description here

This is a very good link .

+1
source share

Gaskets are typically used to provide mocks from assemblies outside your solution, while stubs are used to create class mockups in your solution.

Stub example

 // Create the fake calculator: ICalculator calculator = new Calculator.Fakes.StubICalculator() { // Define each method: Add = (a,b) => { return 25; } }; 

Gasket Example

 //Using shims to control the response to DateTime.Now using (ShimsContext.Create()) { // insert the delegate that returns call for DateTime.Now System.Fakes.ShimDateTime.NowGet = () => new DateTime(2010, 1, 1); MethodThatUsesDateTimeNow(); } 

Courtesy: Exam No. 70-486

0
source share

All Articles