Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

BDD Javascript Testing with Jasmine

Presentation at the Denver Java User's group August 10, 2011

  • Be the first to comment

BDD Javascript Testing with Jasmine

  1. 1. BDD JavascriptTesting with Jasmine Scott Ryan August 2011
  2. 2. AgendaSuites and SpecsMatchersBefore and A"erSpiesAsynchronous SpecsExecution
  3. 3. Javascript Frameworksnode.js sinon.jsphantom.js backbone.jssammy.js spine.jsmustache.js coffeescriptzombie.js microjs.combatman.js yepnope.js
  4. 4. PhilosophyShould not be tied to any browser, &ameworks, platform or host languageShould have idiomatic and unsurprising syntaxShould work anywhere javascript can run including browsers, servers,phones,etcShould not intrude in your applications territory like cluttering globalnamespaceShould play we( with IDE’s
  5. 5. SetupCreate a Spec Use folders to keep organized Don’t bundle into buildCreate one or more html files to exercise your specsDecide on one or more run time strategies Server, Gradle, Maven, Node.js. jsTestDriver, etc.
  6. 6. SpecsDefine your individual testsBegin with it.Set expectations to be satisfiedDisabled by naming xit.
  7. 7. SuitesUsed to group specs togetherNamed using describeCan be nested to any depthDisabled by naming xdescribe
  8. 8. Suites and Spec Example describe("External", function() { it(should default to the velocity of 3830 m/s,function () { var velocity = new Velocity("http://"); expect(velocity.defaultVelocity).toEqual(3830); });});
  9. 9. MatchersMatchers set conditions to be satisfied.not wi( invert the matcherCustom matchers are supported Added to suites with this.addMatchers ca(
  10. 10. Matchers Examplesexpect(x).toEqual(y);expect(x).not.toEqual(y);expect(x).toBeDefined();expect(x).toBeUndefined();expect(fn).toThrow(e);
  11. 11. Before and AfterDescribe functions that run before and/or a"er theexecution of a suite.Specs can contain one or more a"er’s within the specdefinition to be run for that specRunners define before functions to run before each spec ina suite. Runners run before the suite wide before
  12. 12. Before After Exampledescribe("External", function() { // This will run before each spec in this suite beforeEach(function () { console.log("Running before each spec."); alert("before each Spec"); }); // This will run after each spec in this suite. afterEach(function () { console.log("Run after each spec"); alert("after each Spec"); }); it(should default to the velocity in sandstone at 3830 m/s, function () { // This will run after this spec completes. this.after(function() { console.log("Run after spec 1 only") alert("Run after spec 1 only"); }); var velocity = new Velocity("http://"); expect(velocity.defaultVelocity).toEqual(3830); }); it(should fail if we check for the wrong velocity, function () {// This will run after this spec completes. this.after(function() { console.log("Run after spec 2 only which fails") alert("Run after spec 1 only which fails"); }); var velocity = new Velocity("http://"); expect(velocity.defaultVelocity).not.toEqual(3830); });});
  13. 13. SpiesSupport spying, mocking and faking behaviorA spy replaces the function it is spying onSpies work we( for Ajax testingHave a set of special matchers and custom behaviorContain some special propertiesRemoved a"er each spec executes
  14. 14. Spy matchers and responseexpect(x).toHaveBeenCa(ed()expect(x).toHaveBeenCa(edWith(arguments)spyOn(x,‘method’).andThrow(exception);spyOn(x,‘method’).andCa(Through();
  15. 15. Spy propertiesca(CountmostRecentCa(.argsargsForCa([i];
  16. 16. Asynchronous SpecsUsed to test ajax ca(s, ca(backs and eventsruns()waits(timeout)waitsFor(function, message, timeout)
  17. 17. Asynchronous Examplesdescribe("Velocity", function() { it(should set value then wait 250 ms and set a new value, function() { runs(function () { this.velocity = new Velocity("http://"); var that = this; setTimeout(function () { that.velocity.defaultVelocity = 5000; }, 250); }); runs(function () { expect(this.velocity.defaultVelocity).toEqual(3830); }); waits(500); runs(function () { expect(this.velocity.defaultVelocity).toEqual(5000); }); });});
  18. 18. Ajax Callsdescribe("Velocity", function() { it("should be able to get a list of recordings", function() { var recordings; var status = false; var theCall = External.retrieveRecordedJson(); theCall.success(function(inData) { recordings = inData.recordings; status = true; }); waitsFor(function() { return status; }, "Ajax Request never completed", 10000); runs(function () { expect(recordings.length).toBeGreaterThan(0); }); });});
  19. 19. Run EnvironmentsNative BrowserPhantom.js
  20. 20. Questions - scryan7371
  21. 21. Photos