spyOn can be used to stub an existing method by replacing it with a spy The

Spyon can be used to stub an existing method by

This preview shows page 238 - 240 out of 517 pages.

spyOn can be used to stub an existing method by replacing it with a spy. The spy’s behavior can be controlled with functions like and.callThrough , and.returnValue , and so on, as Figure 6.20 shows. Jasmine-jQuery’s HTML fixtures can provide both the “before” content for triggering an AJAX request and the “after” content for testing the results of a successful or failed AJAX request. ELABORATION: Why no Jasmine specs for client-side-only code? We didn’t include specs for the client-side-only example in Section 6.5 for the same reason we didn’t write view specs in Chapter 8 : a widespread practice is to test client-side view behaviors with integration or acceptance level tests, such as Cucumber scenarios using Webdriver (Section 7.6 ). ELABORATION: Testing client-side form validation A common JavaScript use case is to validate entries in a form as the user types, before the form can be submitted. You can test such self-validating forms by creating an HTML fixture representing a form or part of a form, using element .val() to set the value of one or more form inputs, and triggering element .blur() to cause the element to lose focus, simulating the user’s pressing the Tab key or
Image of page 238
using the mouse to navigate to a different form field. You can then either check that the other form fields were properly updated with the new value (by inspecting their element .val() ) or spy on the validation function with .and.callThrough() to ensure that it is called as result of blur. ELABORATION: Fixtures or factories? As Section 8.5 explains, in Rails apps it’s often preferable to use a factory to create necessary test doubles “in place” rather than specifying fixtures. So why do we describe the use of fixtures rather than factories for AJAX testing? One reason is that the tradeoff is different in JavaScript. In the Rails app, fixtures are loaded into the database before tests are run, and various ActiveRecord methods such as find may behave differently when different fixtures are present; therefore fixtures may break test I ndependence. Factories are an appealing alternative in Rails because gems such as FactoryGirl make it easy to instantiate test doubles “just in time” in each test that needs them. In Jasmine, to substitute an HTML “factory” for HTML fixtures, we would use $(”) to create inline HTML elements, but many developers view this as undesirable because mixing HTML markup with JavaScript test code makes the latter hard to read. Jasmine-jQuery provides some simple support for using factories without excessively polluting your test code with HTML markup, as Figure 6.25 shows, but in general we see that fixtures for AJAX testing avoid some of the pitfalls of fixtures for Rails testing. They do, however, introduce a pitfall of their own—the possibility of getting “out of sync” with the app’s views. See Fallacies and Pitfalls for a discussion of this pitfall and its solution. Self-Check 6.7.1. Jasmine-jQuery also supports toContain and toContainText to check if a string of text or HTML occurs within an element. In line 7 of Figure 6.21 , why would it be
Image of page 239
Image of page 240

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