practice theres nothing wrong with creating itblocks for tests you know you

Practice theres nothing wrong with creating itblocks

This preview shows page 309 - 311 out of 517 pages.

practice, there’s nothing wrong with creating it blocks for tests you know you will want to write. Now, though, it’s time to get down to business and start working on the tests. 1. Before you write any new code, write a test for one aspect of the behavior it should have. Since the code being tested doesn’t exist yet, writing the test forces you to think about how you wish the code would behave and interact with its collaborators if it did exist. We call this “exercising the code you wish you had.” 2. Red step: Run the test, and verify that it fails because you haven’t yet implemented the code necessary to make it pass. 3. Green step: Write the simplest possible code that causes this test to pass without breaking any existing tests. 4. Refactor step: Look for opportunities to refactor either your code or your tests—changing the code’s structure to eliminate redundancy, repetition, or other ugliness that may have arisen as a result of adding the new code. The tests ensure that your refactoring doesn’t introduce bugs. 5. Repeat until all behaviors necessary to pass a scenario step are complete. Figure 8.3: The Test-Driven Development (TDD) loop is also known as Red–Green–Refactor because of its skeleton in steps 2–4. The last step assumes you are developing code in order to complete a scenario, such as the one you started in Chapter 7 . The first example (test case) in Figure 8.2 states that the search_tmdb method should call a model method to perform the TMDb search, passing the keywords typed by the user to that method. In Chapter 7 , we modified the index view of RottenPotatoes by adding an HTML form whose submission would be handled by MoviesController#search_tmdb ; the form contained a single text field called search_terms for the user to fill in. Our test case will therefore need to emulate what happens when the user types something into the search_terms field and submits the form. As we know, in a Rails app the params hash is automatically populated with the data submitted in a form so that the controller method can examine it. Happily, RSpec provides a post method that simulates posting a form to a controller action: the first argument is the action name (controller method) that will receive the post, and the second argument is a hash that will become the params seen by the controller action. We can now write the first line of our first spec, as Figure 8.4 shows. As the next screencast shows, though, we must overcome a couple of hurdles just to get to the Red phase of Red–Green–Refactor.
Image of page 309
1 require ’spec_helper’ 2 3 describe MoviesController do 4 describe ’searching TMDb’ do 5 it ’should call the model method that performs TMDb search’ do 6 post :search_tmdb, {:search_terms => ’hardware’} 7 end 8 it ’should select the Search Results template for rendering’ 9 it ’should make the TMDb search results available to that template’ 10 end 11 end Figure 8.4: Filling out the first spec. Whereas a “bare” it (line 8) serves as a placeholder for a yet-to-be-written example, an it accompanied by a do...end block (lines 5–7) is an actual test case.
Image of page 310
Image of page 311

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