martes, 15 de octubre de 2013

Types of Test Double

I am reading the book The RSpec Book: Behaviour-Driven Development with RSpec, Cucumber, and Friends and writing the code that appears in it in the repository code-breaker.
I was in this point. This test in RSpec produces these fails:

Failures:

  1) Codebreaker::Game#start sends a welcome message
     Failure/Error: game.start
       Double "output" received :puts with unexpected arguments
         expected: ("Welcome to Codebreaker!")
              got: ("Enter guess:")
     # ./lib/codebreaker/game.rb:8:in `start'
     # ./spec/codebreaker/game_spec.rb:10:in `block (3 levels) in <module:Codebreaker>'

  2) Codebreaker::Game#start prompts for the first guess
     Failure/Error: game.start
       Double "output" received :puts with unexpected arguments
         expected: ("Enter guess:")
              got: ("Welcome to Codebreaker!")
     # ./lib/codebreaker/game.rb:7:in `start'
     # ./spec/codebreaker/game_spec.rb:16:in `block (3 levels) in <module:Codebreaker>'

The problem is solved here.

Why? I didn't understand. So I asked to . His answer:

There are three types of Test Double:

  • Stub
    • Pasive
    • It doesn't have memory
    • You can configure it to return data
    • They are used normally in the set up of the test
  • Spy
    • Pasive
    • It records what happens with it: Which methods were called and which with parameters
    • They are used in asserts. You ask to it: What happend?
  • Mocks
    • You must say in the setup of the test what things are going to happen.
    • Active: There is a Check of what is happening
    • Two sub types:
      • Strict
        • They fail if it doesn't happen exactly what we said in the setup
      • Lenient
        • They fail if it doesn't happen at least what we said in the setup.


In the test they are using Strict Mocks, and then in the following version they are converting this tests to use Lenient Mocks.
You can see that they aren't calling a should or an expect at the end of the test. The mock already knows what is going to happen.

In the frameworks, these names are mixed, so in some cases, tests that are called stubs aren't really stubs, but a mix of stub and spy, for example.

Wow, what an answer! It was a lot of wisdom, so I wanted to share with you.




No hay comentarios:

Publicar un comentario