BDD Javascript Testing with Jasmine
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

BDD Javascript Testing with Jasmine

on

  • 3,597 views

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

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

Statistics

Views

Total Views
3,597
Views on SlideShare
3,597
Embed Views
0

Actions

Likes
7
Downloads
54
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

CC Attribution-ShareAlike LicenseCC Attribution-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

BDD Javascript Testing with Jasmine Presentation Transcript

  • 1. BDD JavascriptTesting with Jasmine Scott Ryan August 2011
  • 2. AgendaSuites and SpecsMatchersBefore and A"erSpiesAsynchronous SpecsExecution
  • 3. Javascript Frameworksnode.js sinon.jsphantom.js backbone.jssammy.js spine.jsmustache.js coffeescriptzombie.js microjs.combatman.js yepnope.js
  • 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. 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. SpecsDefine your individual testsBegin with it.Set expectations to be satisfiedDisabled by naming xit.
  • 7. SuitesUsed to group specs togetherNamed using describeCan be nested to any depthDisabled by naming xdescribe
  • 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. MatchersMatchers set conditions to be satisfied.not wi( invert the matcherCustom matchers are supported Added to suites with this.addMatchers ca(
  • 10. Matchers Examplesexpect(x).toEqual(y);expect(x).not.toEqual(y);expect(x).toBeDefined();expect(x).toBeUndefined();expect(fn).toThrow(e);
  • 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. 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. 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. Spy matchers and responseexpect(x).toHaveBeenCa(ed()expect(x).toHaveBeenCa(edWith(arguments)spyOn(x,‘method’).andThrow(exception);spyOn(x,‘method’).andCa(Through();
  • 15. Spy propertiesca(CountmostRecentCa(.argsargsForCa([i];
  • 16. Asynchronous SpecsUsed to test ajax ca(s, ca(backs and eventsruns()waits(timeout)waitsFor(function, message, timeout)
  • 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. 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. Run EnvironmentsNative BrowserPhantom.js
  • 20. Questionshttp://pivotal.github.com/jasmine/scott@theryansplace.com@scryan7371github - scryan7371https://github.com/scryan7371/Jasmine-Demo
  • 21. Photos