JSpec
A JavaScript Testing Framework
About me
About me
Lena Herrmann

Ruby, Javascript, CouchDB
About me
Lena Herrmann

Ruby, Javascript, CouchDB

http://lenaherrmann.net
@kilaulena
Upstream Agile GmbH

  http://upstre.am/
Unit Testing
Crosscheck                                  JSSpec
                    RhinoUnit

                     Test.More          ...
Crosscheck                                  JSSpec
                    RhinoUnit

                     Test.More          ...
@tjholowaychuk
What does it look like?
Installation
Download tar from
http://github.com/visionmedia/jspec/downloads
Download tar from
http://github.com/visionmedia/jspec/downloads


or
$ git clone git://github.com/visionmedia/jspec.git
$ sudo gem install jspec
$ sudo gem install jspec



$ jspec init --rails
Run
$ jspec run --rhino
$ jspec run --browsers ff,safari --server
Grammar
Assertions
Grammar
Assertions

'hello'.should.eql('hello')
'hello'.should.eql 'hello'
Grammar
Functions
Grammar
Functions

-{ throw 'foo' }.should.throw_error 'foo'
Matchers
be, eql, equal
Matchers
be, eql, equal

 true.should.be true
 2.should.eql 2
 2.should.eql '2'
 ['foo', 'bar'].should.eql ['foo', 'bar']
...
Matchers
be_a, be_an
Matchers
be_a, be_an

['im', 'an', 'array'].should.be_an Array
'im a string'.should.be_a String
Matchers
have_length
Matchers
have_length

['cookies', 'ice cream'].length.should.eql 2
['cookies', 'ice cream'].should.have_length 2
Matchers
include
Matchers
include

 ['cookies', 'apples'].should.include 'apples'
 ['cookies', 'apples'].should.not.include 'bread'
Matchers
have_property
Matchers
have_property

{ name: 'lena' }.should.have_property 'name'
{ name: 'lena' }.should.have_property 'name', 'lena'
Matchers
jQuery
Matchers
jQuery

$('form#login .agree').should.not.be_clicked
Matchers
jQuery

$('form#login .agree').should.not.be_clicked
$('form').should.have_id 'login'
Matchers
jQuery

$('form#login .agree').should.not.be_clicked
$('form').should.have_id 'login'

have_tag, have_child, have...
Fixtures
fixtures/storyboard.html:
Stubbing

person.stub('age').and_return(22)
Mocking
mock_request()
Thank you!
JSpec - A Javascript Unit Testing Framework
JSpec - A Javascript Unit Testing Framework
JSpec - A Javascript Unit Testing Framework
JSpec - A Javascript Unit Testing Framework
JSpec - A Javascript Unit Testing Framework
JSpec - A Javascript Unit Testing Framework
JSpec - A Javascript Unit Testing Framework
JSpec - A Javascript Unit Testing Framework
JSpec - A Javascript Unit Testing Framework
JSpec - A Javascript Unit Testing Framework
JSpec - A Javascript Unit Testing Framework
JSpec - A Javascript Unit Testing Framework
JSpec - A Javascript Unit Testing Framework
Upcoming SlideShare
Loading in …5
×

JSpec - A Javascript Unit Testing Framework

5,083 views

Published on

JSpec is a great framework for writing unit tests for your Javascript code. I show you why I chose it and how you install and use it.

Published in: Technology
3 Comments
5 Likes
Statistics
Notes
No Downloads
Views
Total views
5,083
On SlideShare
0
From Embeds
0
Number of Embeds
57
Actions
Shares
0
Downloads
66
Comments
3
Likes
5
Embeds 0
No embeds

No notes for slide



  • We take TDD very seriously
    Working on my thesis there:
  • My thesis: an outliner, a couchapp
    Have a project in mind where Javascript is the main language
    Application is served by CouchDB - no other server, other language involved
  • Not about full stack integration test

  • Tiny (15 kb compressed, 1600-ish LOC)

  • project: 1 and a half years old, more than 30 forks on git
    in active development

  • no parentheses, "function" expressions, curly brackets
    Cascading before/after hooks -- Nested describe blocks (DRY, organization)
    Textmate bundle, Syntax hightlighting
  • grammar-less alternative

  • Two ways to install jspec:
    1st, when project should come without prerequisites


  • Just drop the files into your project.
  • 2nd: - when to be used with CI
    - when you DON'T want to test the DOM
    Rails command copies a jspec template folder into your project

  • Two ways to run the tests
  • Open the html file in any browser.
  • Also show only failing tests
  • Not so nice: instead of test groups, you have to put tests into different files
  • Java implementation of Javascript (like Spidermonkey is in C)

  • When you have the gem already installed:
    opens browsers in the background, report back to console



  • equal = object identity ===




  • there's many more
  • there's many more
  • there's many more
  • sometimes you want to test DOM manipulation




  • Mocking:
    - Possible to include Mocking frameworks
    - or as its JS, just override the methods

  • JSpec - A Javascript Unit Testing Framework

    1. 1. JSpec A JavaScript Testing Framework
    2. 2. About me
    3. 3. About me Lena Herrmann Ruby, Javascript, CouchDB
    4. 4. About me Lena Herrmann Ruby, Javascript, CouchDB http://lenaherrmann.net @kilaulena
    5. 5. Upstream Agile GmbH http://upstre.am/
    6. 6. Unit Testing
    7. 7. Crosscheck JSSpec RhinoUnit Test.More jsUnity jsUnitTest J3Unit QUnit JSUnit DOH Test.Simple Jasmine JSTest TestCase JSNUnit screw-unit YUI Test
    8. 8. Crosscheck JSSpec RhinoUnit Test.More jsUnity jsUnitTest J3Unit QUnit JSUnit JSpec DOH Test.Simple Jasmine JSTest TestCase JSNUnit screw-unit YUI Test
    9. 9. @tjholowaychuk
    10. 10. What does it look like?
    11. 11. Installation
    12. 12. Download tar from http://github.com/visionmedia/jspec/downloads
    13. 13. Download tar from http://github.com/visionmedia/jspec/downloads or $ git clone git://github.com/visionmedia/jspec.git
    14. 14. $ sudo gem install jspec
    15. 15. $ sudo gem install jspec $ jspec init --rails
    16. 16. Run
    17. 17. $ jspec run --rhino
    18. 18. $ jspec run --browsers ff,safari --server
    19. 19. Grammar Assertions
    20. 20. Grammar Assertions 'hello'.should.eql('hello') 'hello'.should.eql 'hello'
    21. 21. Grammar Functions
    22. 22. Grammar Functions -{ throw 'foo' }.should.throw_error 'foo'
    23. 23. Matchers be, eql, equal
    24. 24. Matchers be, eql, equal true.should.be true 2.should.eql 2 2.should.eql '2' ['foo', 'bar'].should.eql ['foo', 'bar'] ['foo', 'bar'].should.not.equal ['foo', 'bar']
    25. 25. Matchers be_a, be_an
    26. 26. Matchers be_a, be_an ['im', 'an', 'array'].should.be_an Array 'im a string'.should.be_a String
    27. 27. Matchers have_length
    28. 28. Matchers have_length ['cookies', 'ice cream'].length.should.eql 2 ['cookies', 'ice cream'].should.have_length 2
    29. 29. Matchers include
    30. 30. Matchers include ['cookies', 'apples'].should.include 'apples' ['cookies', 'apples'].should.not.include 'bread'
    31. 31. Matchers have_property
    32. 32. Matchers have_property { name: 'lena' }.should.have_property 'name' { name: 'lena' }.should.have_property 'name', 'lena'
    33. 33. Matchers jQuery
    34. 34. Matchers jQuery $('form#login .agree').should.not.be_clicked
    35. 35. Matchers jQuery $('form#login .agree').should.not.be_clicked $('form').should.have_id 'login'
    36. 36. Matchers jQuery $('form#login .agree').should.not.be_clicked $('form').should.have_id 'login' have_tag, have_child, have_text, have_class, be_visible, be_selected, have_alt ...........
    37. 37. Fixtures
    38. 38. fixtures/storyboard.html:
    39. 39. Stubbing person.stub('age').and_return(22)
    40. 40. Mocking mock_request()
    41. 41. Thank you!

    ×