JavaScript Isn't a Toy - It's Time it was Tested

606 views

Published on

JavaScript is no longer a toy language. Your applications have growing dependencies that stretch beyond client-side validation. That means it's time JavaScript was effectively tested. This session will look at the tools that are available, including JS-Unit, which lets you exercise your JavaScript code with familiar Apex-style unit tests, and JSSpec, which enables Behavor-Driven Design.

Published in: Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
606
On SlideShare
0
From Embeds
0
Number of Embeds
34
Actions
Shares
0
Downloads
14
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • expect(x).toEqual(y); compares objects or primitives x and y and passes if they are equivalentexpect(x).toBeNull(); passes if x is nullexpect(x).toContain(y); passes if array or string x contains yexpect(x).toBeLessThan(y); passes if x is less than yexpect(x).toBeGreaterThan(y); passes if x is greater than yexpect(fn).toThrow(e); passes if function fn throws exception e when executed
  • expect(x).toHaveBeenCalled() passes if x is a spy and was calledexpect(x).toHaveBeenCalledWith(arguments) passes if x is a spy and was called with the specified argumentsexpect(x).not.toHaveBeenCalled() passes if x is a spy and was not calledexpect(x).not.toHaveBeenCalledWith(arguments) passes if x is a spy and was not called with the specified arguments
  • Spies have some useful properties:callCount: returns number of times spy was calledmostRecentCall.args: returns argument array from last call to spy.argsForCall[i] returns arguments array for call i to spy.Spies are automatically removed after each spec. They may be set in the beforeEach function.
  • The callBack() is not actually called by the asyncMethod() it is only used as a method parameterHowever we do call it ourselves and we access it by inspecting the arguments used by the most recent callThen we verify our expected that the callBack() has now been called
  • waitsFor() provides a better interface for pausing your spec until some other work has completed. Jasmine will wait until the provided function returns true before continuing with the next block. This may mean waiting an arbitrary period of time, or you may specify a maxiumum period in milliseconds before timing out:In this example, we create a spreadsheet and fill it with some sample data. We then ask the spreadsheet to start calculating its total, which presumably is a slow operation and therefore happens asynchronously. We ask Jasmine to wait until the spreadsheet’s calculation work is complete (or up to 10 seconds, whichever comes first) before continuing with the rest of the spec. If the calculation finishes within the allotted 10 seconds, Jasmine continues on to the final runs() block, where it validates the calculation. If the spreadsheet hasn’t finished calculations within 10 seconds, the spec stops and reports a spec failure with the message given in the waitsFor() block.In this example, we create a spreadsheet and fill it with some sample data. We then ask the spreadsheet to start calculating its total, which presumably is a slow operation and therefore happens asynchronously. We ask Jasmine to wait until the spreadsheet’s calculation work is complete (or up to 10 seconds, whichever comes first) before continuing with the rest of the spec. If the calculation finishes within the allotted 10 seconds, Jasmine continues on to the final runs() block, where it validates the calculation. If the spreadsheet hasn’t finished calculations within 10 seconds, the spec stops and reports a spec failure with the message given in the waitsFor() block.
  • Force failures are exactly what they sound like. You want to force a failure. You are able to pass along a specific error message to be output.
  • Some of the benefits of YUI testSome things you still need to deal with…
  • Nice thing is standardized way to mimic user inputs that works everywhere
  • Update your tests to run correctly in each browserYAHOO.env.ua.{browser}YAHOO.env.ua.gecko
  • I like the approach because we can save the data into a custom object for reporting purposes
  • Obvious benefits include creating new test executions relating user story record to test case
  • ×