Technically in this case it would be OK to omit andreturn since this example

Technically in this case it would be ok to omit

This preview shows page 312 - 314 out of 517 pages.

Technically, in this case it would be OK to omit and_return , since this example isn’t checking the return value, but we included it for illustrative purposes. This new version of the test fails because we established an expectation that search_tmdb would call find_in_tmdb , but the search_tmdb isn’t even written yet. Therefore the last step is to go from Red to Green by adding just enough code to search_tmdb to pass this test. We say the test drives the creation of the code, because adding to the test results in a failure that must be addressed by adding new code in the model. Since the only thing this particular example is testing is the method call to find_in_tmdb , it suffices to add to search_tmdb the single line of code we had in mind as “the code we wished we had”: 1 @movies = Movie.find_in_tmdb(params[:search_terms]) If TDD is new to you, this has been a lot to absorb, especially when testing an app using a powerful framework such as Rails. Don’t worry—now that you have been exposed to the main concepts, the next round of specs will go faster. It takes a bit of faith to jump into this system, but we have found that the reward is well worth it. Read the summary below and consider having a sandwich and reviewing the concepts in this section before moving on. Summary The TDD cycle of Red–Green–Refactor begins with writing a test that fails because the subject code it’s testing doesn’t exist yet (Red) and then adding the minimum code necessary to pass just that one example (Green). Seams let you change the behavior of your application in a particular place without editing in that place. Typical test setup often establishes seams by using mock or its alias double to create test double objects, or by using should_receive...and_return to stub (replace and control the return value of) collaborator methods. Mocks and stubs are seams that help with testability by isolating the behavior of the code being tested from the behavior of its collaborators. Each example sets up preconditions, executes the subject code, and asserts something about the results. Assertions such as should , should_not , should_receive , and with make tests S elf- checking, eliminating the need for a human programmer to inspect test results.
Image of page 312
After each test, an automatic teardown destroys the mocks and stubs and unsets any expectations, so that tests remain I ndependent. ELABORATION: Seams in other languages In non-object-oriented languages such as C, seams are hard to create. Since all method calls are resolved at link time, usually the developer creates a library containing the “fake” (test double) version of a desired method, and carefully controls library link order to ensure the test-double version is used. Similarly, since C data structures are accessed by reading directly from memory rather than calling accessor methods, data structure seams (mocks) are usually created by using preprocessor directives such as #ifdef TESTING to compile the code differently for testing vs. production use.
Image of page 313
Image of page 314

You've reached the end of your free preview.

Want to read all 517 pages?

  • Spring '19
  • Dr.Marcos

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors