Test Doubles - Testing Flexible Interactions with Fakes

This topic is for the [Testing Flexible Interactions with Fakes] exercise in the [Test Doubles] trail. Post any questions, corrections, or pointers you have to share with other Upcase subscribers.
[Testing Flexible Interactions with Fakes]: https://exercises.upcase.com/exercises/testing-flexible-interactions-with-fakes
[Test Doubles]: Test Doubles | RSpec Stub Online Tutorial by thoughtbot

A few questions about the example given before the exercise

  • In the FakeSearch class, why in the methods for author_word and title_word do you write
    self.class.new to merge the @criteria hash instead of just @criteria.merge()?
    Also why are they there? It doesn’t seem like they are used for the test.

I’m guessing that SearchForm must initialize a new instance of Search with the arguments (title:, author:) that were passed to SearchForm.new which we replace with FakeSearch.new

FakeSearch has a method results which instantiates a new instance of the Results class with @criteria which is the hash that was passed in as arguments to SearchForm.new

  • Does SearchForm have a results method also? Since we are calling results on the object that gets returned from SearchForm.new(title: "one two", author: "Billy Idol")?

  • It seems that form = SearchForm.new(title: "one two", author: "Billy Idol") should be form = Search.new(title: "one two", author: "Billy Idol"). Am I missing something?


A few questions about the exercise instructions.

Change the log level for a successful signup from :info to :debug, changing the test first so as to follow test-driven development. Notice the failure message you receive after updating the expectation.

  • In these instructions what is “log level”? Is it the method that logger expects to receive?

Assuming that “log level” means the message that logger is supposed to receive I changed the expected message in the test and I get this error

 Failure/Error: expect(logger).to have_received(:debug).
       (Double "logger").debug("Created user user@example.com with account Example")
           expected: 1 time with arguments: ("Created user user@example.com with account Example")
           received: 0 times
  • All I notice about is that I’m getting the error I’m suppose to get because logger is no longer receiving the :info message. Is there something else I’m supposed to notice here?

With these instructions:

Change the log level for a failed signup from :error to :fatal. Again, change the test first so that you see the failure message. Adjust the fake until the failure message makes it very clear what you must change to make the test pass, and then make the test pass.

The “failure message” is that from Rspec or is it the fake logger? I’m assuming it is from the fake logger because I don’t see how adjusting the fake could change the error message I get from Rspec to tell me how to get the test to pass.