If the fixtures file is ever changed so that the number of items initially

If the fixtures file is ever changed so that the

This preview shows page 321 - 323 out of 517 pages.

If the fixtures file is ever changed so that the number of items initially populating that table changes, this test may suddenly start failing because its assumptions about the initial state of the table no longer hold. In contrast, a factory can be used to quickly create only those objects needed for each test or example group on demand, so no test needs to depend on any global “initial state” of the database. 8.6 Implicit Requirements and Stubbing the Internet We’ve now created two of the three parts of the new “Search TMDb” feature: we created the view in Chapter 7 and we used TDD to drive the creation of the controller action in the previous sections. All that remains to finish the user story we started in Chapter 7 is the model method find_in_tmdb , which actually uses Service-Oriented Architecture technology to communicate with TMDb. Using TDD to drive its implementation will go quickly now that we know the basics. By convention over configuration, specs for the Movie model go in spec/models/movie_spec.rb . Figure 8.12 shows the happy path for calling find_in_tmdb , which describes what happens when everything works correctly. (Complete specs must also cover the sad paths, as we’ll soon see.) Inside the overall describe Movie , we’ve added a nested describe block for the keyword-search function. Our first spec says that when find_in_tmdb is called with a string parameter, it should pass that string parameter to the TMDb gem’s Tmdb::Movie.find class method. This spec should immediately fail because we haven’t defined find_in_tmdb yet, so we are at the Red stage already. Of course, at this stage, find_in_tmdb is trivial, so the bottom of Figure 8.12 shows its initial implementation that gets us from Red to Green.
Image of page 321
Where’s the gem? Don’t we need to require ’themoviedb’ somewhere in the model definition or the specs? For a non-Rails app, yes, but Rails automatically require s any gems you specify in the Gemfile. 1 require ’spec_helper’ 2 3 describe Movie do 4 describe ’searching Tmdb by keyword’ do 5 it ’should call Tmdb with title keywords’ do 6 Tmdb::Movie.should_receive(:find).with(’Inception’) 7 Movie.find_in_tmdb(’Inception’) 8 end 9 end 10 end 1 class Movie < ActiveRecord::Base 2 3 def self.find_in_tmdb(string) 4 Tmdb::Movie.find(string) 5 end 6 7 # rest of file elided for brevity 8 end Figure 8.12: (Top) the happy path spec for using the TMDb gem; (bottom) Initial happy path implementation driven by happy path spec. Why doesn’t the controller method search_tmdb just call Tmdb::Movie.find directly, rather than passing an argument to the seemingly “intermediate” method find_in_tmdb ? There are two reasons. First, if the TMDb gem’s API changes, perhaps to accommodate a change to the TMDb service API itself, we can insulate the controller from those changes because all the knowledge of how to use the gem to communicate with the service is encapsulated inside the Movie model class. This indirection is
Image of page 322
Image of page 323

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