Standards for Mock Objects

In Roy Oscherove’s book [Unit Testing] [1] he explains that one unit test should contain from 0 to 1 mocks. It assumes that if your test does not approve the layout, then do not use the layout at all. He further demonstrates how to use an insulating structure to create plugs that were created similar to poppy. It does not impose any implied limit on the number of blanks created for each test.

My question is this: can these recommendations apply to all isolation structures (or all popular C # frameworks)? In other words, is there a structure that can only generate mocks - not stubs? Is there an isolation environment that doesn't distinguish mocks from stubs?

I'm just wondering how easily Osherove recommendations can be converted to coding standards.

[1]: http: // the system under test is not even tested, and the data returned from mocks is checked.

+4
source share
7 answers

There are frameworks such as Moq that do not distinguish between them. FakeItEasy goes even further and calls all fake objects.

Yes, they can be applied because layouts are just smart stubs. It doesn’t really matter if a stub is called as a layout, unless you approve more than one layout. In other words, this recommendation is related to the approval of only one thing per test. Explicitly distinguishing between layouts and stubs is not that important.

+1
source

This corresponds directly to Osherov’s recommendation that you perform only one statement per test. In his vocabulary, a stub is a class that provides fake login to the system under test, and mock is a class that allows you to test output from the system under test (using a fake class).

Regardless of whether the environment uses a similar naming convention to its developers, the standard (if you agree with its recommendation) should be that for each test only one statement is executed, and in case of approval, a fake object is required - only one fake The object must be tested for each test.

Of course, not everyone agrees with his recommendation, so not everyone does.

+2
source

Recently, Philippe Calzado wrote about this: http://fragmental.tw/2010/12/14/one-mock-per-test-considered-not-awesome/ . In short, he says that focusing on how many pieces / dead ends are in your test distracts you from what should be the main problem: writing good specifications.

+2
source
 > can those recommendations > [a single unit test should contain between 0 and 1 mocks] > be applied to all isolation frameworks ? 

I think: most of the time. Yes , if you accept these definitions.

  • Unittest = Testing separately (otherwise it is not Unittest)
  • One Unittest for one function
  • a stub or fake is an object that allows you to isolate, but which does not have a check function and
  • the layout is a stub with additional functionality that also allows you to check
+1
source

I use Moq for my Mocking Get it here

I'm not sure what exactly you mean by "stubbing", but I guess this is probably something like Moles from Microsoft , which is pretty cool. Here

Both are really interesting and very easy to use.

0
source

This rule should be considered as "Learning Wheels". Obviously, the test, which includes a lot of cigarette butts and layouts, has lost the plot, but insisting on one statement or waiting for the test is too strict. As the Calçado link points out, it’s important that there is one concept for the test, which may include several statements or expectations to do so. The last thing you need to do is to ensure compliance with such a standard.

One more thing, we really have to talk about “expectations” here, about individual interactions, and not about all mock objects. In practice, they are often the same, but this erodes the concepts.

0
source

I am using Rhino mock for my C # test. You can create mocks or stubs with it. I recommend you take a look at this structure: http://www.ayende.com/projects/rhino-mocks.aspx

-1
source

Source: https://habr.com/ru/post/1334363/


All Articles