Why do we need mocking frameworks?

后端 未结 11 1737
清歌不尽
清歌不尽 2020-12-30 03:00

I have worked with code which had NUnit test written. But, I have never worked with mocking frameworks. What are they? I understand dependency injection and how it helps to

相关标签:
11条回答
  • 2020-12-30 03:11

    Mock objects take the place of any large/complex/external objects your code needs access to in order to run.

    They are beneficial for a few reasons:

    • Your tests are meant to run fast and easily. If your code depends on, say, a database connection then you would need to have a fully configured and populated database running in order to run your tests. This can get annoying, so you create a replace - a "mock" - of the database connection object that just simulates the database.

    • You can control exactly what output comes out of the Mock objects and can therefore use them as controllable data sources to your tests.

    • You can create the mock before you create the real object in order to refine its interface. This is useful in Test-driven Development.

    0 讨论(0)
  • 2020-12-30 03:11

    I first grok'd why I needed a mocking framework when I compared writing test doubles by hand for a set of unit tests (each test needed slightly different behaviour so I was creating subclasses of a base fake type for each test) with using something like RhinoMocks or Moq to do the same work.

    Simply put it was much faster to use a framework to generate all of the fake objects I needed rather than writing (and debugging) my own fakes by hand.

    0 讨论(0)
  • 2020-12-30 03:19

    You can create mock objects by hand and use them during testing using Dependency Injection frameworks...but letting a mocking framework generate your mock objects for you saves time.

    As always, if using the framework adds too much complexity to be useful then don't use it.

    0 讨论(0)
  • 2020-12-30 03:19

    The only reason to use a mocking library is that it makes mocking easier.

    Sure, you can do it all without the library, and that is fine if it's simple, but as soon as they start getting complicated, libraries are much easier.

    Think of this in terms of sorting algorithms, sure anyone can write one, but why? If the code already exists and is simple to call... why not use it?

    0 讨论(0)
  • 2020-12-30 03:20

    Well mocking frameworks make my life much easier and less tedious so I can spend time on actually writing code. Take for instance Mockito (in the Java world)

     //mock creation
     List mockedList = mock(List.class);
    
     //using mock object
     mockedList.add("one");
     mockedList.clear();
    
     //verification
     verify(mockedList).add("one");
     verify(mockedList).clear();
    
     //stubbing using built-in anyInt() argument matcher
     when(mockedList.get(anyInt())).thenReturn("element");
    
     //stubbing using hamcrest (let's say isValid() returns your own hamcrest matcher):
     when(mockedList.contains(argThat(isValid()))).thenReturn("element");
    
     //following prints "element"
     System.out.println(mockedList.get(999));
    

    Though this is a contrived example if you replace List.class with MyComplex.class then the value of having a mocking framework becomes evident. You could write your own or do without but why would you want to go that route.

    0 讨论(0)
  • 2020-12-30 03:24

    I'd claim you don't. Writing test doubles isn't a large chore in 9 times out of 10. Most of the time it's done almost entirely automatically by just asking resharper to implement an interface for you and then you just add the minor detail needed for this double (because you aren't doing a bunch of logic and creating these intricate super test doubles, right? Right?)

    "But why would I want my test project bloated with a bunch of test doubles" you may ask. Well you shouldn't. the DRY principle holds for tests as well. Create GOOD test doubles that are reusable and have descriptive names. This makes your tests more readable too.

    One thing it DOES make harder is to over-use test doubles. I tend to agree with Roy Osherove and Uncle Bob, you really don't want to create a mock object with some special configuration all that often. This is in itself a design smell. Using a framework it's soooo easy to just use test doubles with intricate logic in just about every test and in the end you find that you haven't really tested your production code, you have merely tested the god-awful frankenstein's monsteresque mess of mocks containing mocks containing more mocks. You'll never "accidentally" do this if you write your own doubles.

    Of course, someone will point out that there are times when you "have" to use a framework, not doing so would be plain stupid. Sure, there are cases like that. But you probably don't have that case. Most people don't, and only for a small part of the code, or the code itself is really bad.

    I'd recommend anyone (ESPECIALLY a beginner) to stay away from frameworks and learn how to get by without them, and then later when they feel that they really have to they can use whatever framework they think is the most suitable, but by then it'll be an informed desicion and they'll be far less likely to abuse the framework to create bad code.

    0 讨论(0)
提交回复
热议问题