attrhref is the correct JavaScript code to extract the AJAX URL from the table

Attrhref is the correct javascript code to extract

This preview shows page 236 - 239 out of 517 pages.

$(this).attr(’href’) is the correct JavaScript code to extract the AJAX URL from the table.
Image of page 236
Figure 6.23: Top: Normally, our getMovieInfo function calls jQuery’s ajax , which calls xhr in the browser’s JSAPI, which sends the request to the server. The server’s reply triggers callback logic in the browser’s JSAPI, which calls an internal jQuery method that eventually calls our showMovieInfo callback. If we stub the ajax function, we can cause showMovieInfo to be called immediately; we can also stub “farther away” by stubbing xhr (using the Jasmine-Ajax plugin), causing the jQuery internal dispatcher to be called immediately. Bottom: Graphical representation of the discussion accompanying Figure 8.16 in Section 8.6 . Figure 6.23 shows the similarity between the challenges of stubbing the Internet for testing AJAX and stubbing the Internet for testing code in a Service-Oriented Architecture (Section 8.6 ). As you can see, in both scenarios, the decision of where to stub depends on how much of the stack we want to exercise in our tests. Line 19 reads in a fixture that will take the place of the ajax response from the movies controller show action, see Figure 6.24 . In lines 20–22 we see the use fo the callFake function to not only intercept an AJAX call, but also to fake a successful response using the fixture. This and the triggering of the AJAX call (line 23) is repeated for each of the following two tests which check that both the #movieInfo popup is visible (line 26) and that it contains text from the movie description (line 29). 1 <p>Casablanca is a classic and iconic film starring 2 Humphrey Bogart and Ingrid Bergman.</p> 3 <a href="" id="closeLink">Close</a>
Image of page 237
Figure 6.24: This HTML fixture mimics the ajax response from the movies controller show action; it goes in spec/javascripts/fixtures/movie_info.html . This concise introduction, along with the summary tables in this section, should get you started using BDD for your JavaScript code. The best sources of complete documentation for these tools are the Jasmine documentation and the Jasmine jQuery add-on documentation. 1 describe(’element sanitizer’, function() { 2 it(’removes IMG tags from evil HTML’, function() { 3 setFixtures(sandbox({class: ’myTestClass’})); 4 $(’.myTestClass’).text("Evil HTML! <img src=’;"); 5 $(’.myTestClass’).sanitize(); 6 expect($(’.myTestClass’).text()).not.toContain(’<img’); 7 }); 8 }); Figure 6.25: Jasmine-jQuery’s sandbox method creates a new HTML div with the given attributes; its id defaults to sandbox if not given. Lines 4–5 use the sandbox-created element. The sandbox can be used to temporarily contain elements constructed in a factory-like way without “polluting” the test code with HTML markup. Summary of Jasmine BDD for JavaScript: Like RSpec, Jasmine specs are anonymous functions accompanied by a descriptive string. They are introduced by the Jasmine function it , can be grouped with (nested) describe blocks that have associated beforeEach and afterEach (test setup and teardown) calls.
Image of page 238
Image of page 239

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