Js contains tests for

This preview shows page 231 - 234 out of 517 pages.

spec/javascripts/movie_popup_spec.js contains tests for app/assets/javascripts/movie_popup.js ; spec/javascripts/moviePopupSpec.js contains tests for app/assets/javascripts/moviePopup.js Configuration file .rspec spec/javascripts/support/jasmine.yml
Image of page 231
Run all tests rake spec rake jasmine , then visit ; or rake jasmine:ci to run once using Selenium/Webdriver and capture the output; or use jasmine-headless-webkit to run from command line with no browser Figure 6.17: Comparison of setting up and using Jasmine and RSpec. All paths are relative to the app root and all commands should be run from the app root. As you can see, the main difference is the use of lower_snake_case for filenames and method names in Ruby, versus lowerCamelCase in JavaScript. 1 rails generate jasmine:install 2 mkdir spec/javascripts/fixtures 3 curl - jquery/master/lib/jasmine-jquery.js > spec/javascripts/helpers/jasmine-jquery.js 4 git add spec/javascripts Figure 6.18: Creating the Jasmine-related directories in your app. Line 1 creates a spec/javascripts directory where our tests will go, with subdirectories support and helper analogous to RSpec’s setup (Section 8.2 ). Line 2 adds a subdirectory for fixtures (Section 8.5 ). Line 3 installs an add-on to Jasmine that provides extra support for testing jQuery-based code and for using fixtures. Line 4 adds these new JavaScript TDD files to your project. To start using Jasmine, add gem ’jasmine’ to your Gemfile and run bundle as usual, then run the commands in Figure 6.18 from your app’s root directory. For esoteric reasons, we can’t run a completely empty Jasmine test suite, so create the file spec/javascripts/sanity_check_spec.js containing the following code: 1 describe(’Jasmine sanity check’, function() { 2 it(’works’, function() { expect(true).toBe(true); }); 3 }); To run tests, type rake jasmine , and once it’s running browse to to see the test results. From now on, when we change any code in app/assets/javascripts or tests in spec/javascripts , just reload the browser page to rerun all the tests. Self-checking? rake jasmine:ci runs the Jasmine suite just once using Webdriver and collects the output, and the (faster) jasmineheadlesswebkit or the Jasmine-Rails gem run tests without the overhead of running a browser. Either method would work in an automated continuous integration (CI) environment (Section 12.3 ) where there’s no human watching the test results on the browser page. Testing AJAX code must address two problems, and if you have read about TDD in Chapter 8 , you’re already familiar with the solutions to both. First, just as we did in Section 8.6 , we must be able to “stub out the Internet” by intercepting AJAX calls, so that we can return “canned” AJAX responses and test
Image of page 232
our JavaScript code in isolation from the server. We will solve this problem using stubs. Second, our JavaScript code expects to find certain elements on the rendered page, but as we just saw, when running Jasmine tests the browser is viewing the Jasmine reporting page rather than our app. Happily, we can
Image of page 233
Image of page 234

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