Self Check 861 Given that failing to initialize a valid API key causes

Self check 861 given that failing to initialize a

This preview shows page 327 - 329 out of 517 pages.

Self-Check 8.6.1. Given that failing to initialize a valid API key causes themoviedb gem to raise an exception, why doesn’t line 7 of Figure 8.13 raise an exception? Line 6 replaces the Tmdb::Movie.find call with a stub, preventing the “real” method from executing and raising an exception. Self-Check 8.6.2. Considering line 10 of Figure 8.13 , suppose we didn’t wrap the call to find_in_tmdb in a lambda-expression. What would happen and why? If find_in_tmdb correctly raises the exception, the spec will fail because the exception will stop the run. If find_in_tmdb incorrectly fails to raise an exception, the spec will fail because the assertion should raise_error expects one. Therefore the test would always fail whether find_in_tmdb was correct or not. Self-Check 8.6.3. Name two likely violations of FIRST that arise when unit tests actually call an external service as part of testing. The test may no longer be Fast, since it takes much longer to call an external service than to compute locally. The test may no longer be Repeatable, since circumstances beyond our control could affect its outcome, such as the temporary unavailability of the external service. Structure of test cases: before(:each) do. . . end Set up preconditions executed before each spec (use before(:all) to do just once, at your own risk) it ’should do something’ do. . . end A single example (test case) for one behavior describe ’collection of behaviors’ do. . . end Groups a set of related examples Mocks and stubs: m=mock(’movie’) Creates a mock object with no predefined methods m.stub(:rating).and_return(’R’) Replaces the existing rating method on m , or defines a new rating method if none exists, that returns the canned response ’R’ m=mock(’movie’, :rating=>’R’) Shortcut that combines the 2 previous examples Movie.stub(:find).and_return(@fake_movie) Forces @fake_movie to be returned if Movie.find is called, but doesn’t require that it be called
Image of page 327
Useful methods and objects for controller specs: Your specs must be in the spec/controllers subdirectory for these methods to be available. post ’/movies/create’, {:title=>’Milk’, :rating=>’R’} Causes a POST request to /movies/create and passes the given hash as the value of params . get , put , delete also available. response.should render_template(’show’) Checks that the controller action renders the show template for this controller’s model response.should redirect_to(:controller => ’movies’, :action => ’new’) Checks that the controller action redirects to MoviesController#new rather than rendering a view Figure 8.17: Some of the most useful RSpec methods introduced in this chapter. See the full RSpec documentation for details and additional methods not listed here. Assertions on method calls: can also negate, e.g. should_not_receive Movie.should_receive(:find).exactly(2).times Stubs Movie.find and ensures it’s called exactly twice (omit exactly if you don’t care how many calls; at_least() and at_most() also available Movie.should_receive(:find).with(’Milk’,’R’) Checks that Movie.find is called with exactly 2 arguments having these values Movie.should_receive(:find).with(anything())
Image of page 328
Image of page 329

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