In the rest of this chapter we will use TDD to develop the searchtmdb method

In the rest of this chapter we will use tdd to

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

(showing red) when you tried to actually run the scenario. In the rest of this chapter we will use TDD to develop the search_tmdb method. Bar#foo is idiomatic Ruby notation denoting the instance method foo of class Bar . The notation Bar.foo denotes the class method foo . In the MVC architecture, the controller’s job is to respond to a user interaction, call the appropriate model method(s) to retrieve or manipulate any necessary data, and generate an appropriate view. We might therefore describe the desired behavior of our as-yet-nonexistent controller method as follows: It should call a model method to perform the TMDb search, passing it the search terms typed by the user.
Image of page 306
It should select the Search Results HTML view (in Rails parlance, the Search Results template ) for rendering. It should make the TMDb search results available to that template. Note that none of the methods or templates in this list of desiderata actually exists yet! That is the essence of TDD: write a concrete and concise list of the desired behaviors (the spec), and use it to drive the creation of the methods and templates. 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’ 6 it ’should select the Search Results template for rendering’ 7 it ’should make the TMDb search results available to that template’ 8 end 9 end Figure 8.2: Skeleton of RSpec examples for MoviesController#search_tmdb . By convention over configuration, the specs for app/controllers/movies_controller.rb are expected to be in spec/controllers/movies_controller_spec.rb , and so on. (Use Pastebin to copy-and-paste this code.) You can see what they are in spec/spec_helper.rb . Figure 8.2 shows how we would express these requirements in RSpec. As in Chapter 3 , we encourage you learn by doing. Before creating this file, you need to set up RottenPotatoes to use RSpec for testing, which requires four steps: 1. In the group :test block in the Gemfile, add gem ’rspec-rails’ 2. Since our app will be using and relying on themoviedb gem as well, add gem ’themoviedb’ outside of any group block (since the gem will be used in the production, development, and testing environments) 3. As always when modifying the Gemfile, run bundle install --without production 4. In the app root directory of RottenPotatoes, run rails generate rspec:install to set up the files and directories RSpec needs. This step also creates a default spec/spec_helper.rb file that sets up some helper methods we will use in all the examples. You’re now ready to create the file spec/controllers/movies_controller_spec.rb as shown in Figure 8.2 . Line 1 loads some helper methods that will be used by all RSpec tests; in general, for Rails apps this will be the first line of any specfile. Line 3 says that the following specs describe the behavior of the MoviesController class. Because this class has several methods, line 4 says that this first set of specs describes the behavior of the method that searches TMDb. As you can see, describe
Image of page 307
can be followed by either a class name or a descriptive documentation string.
Image of page 308
Image of page 309

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