際際滷

際際滷Share a Scribd company logo
Testing JS with Jasmine
Jasmine itself and useful instruments
JavaScript



CoffeeScript
Jasmine instruments


Suites            beforeEach
Specs             afterEach
Matchers          Spies
Custom Matchers   Asynchronous specs
Jasmine instruments Spies
             spyOn(...).andCallThrough()
             spyOn(...).andReturn(arguments)
Spying       spyOn(...).andThrow(exception)
Mocking      spyOn(...).andCallFake(function)
Faking       callCount
             mostRecentCall.args
             argsForCall[i]
Jasmine Asynchronous specs



runs(function)
waits(timeout)
waitsFor(function, optional message, optional timeout)
Jasmine minuses

Runs in browser
+R (F5)
No 鍖xtures
Static JS vs AssetPipeline
AJAX mocking isnt friendly
Guard
Guard
Guard is a command line tool
to easily handle events on 鍖le modi鍖cations.

  gem 'guard-rspec'
  gem 'guard-annotate'
  gem 'guard-bundler'
  gem 'guard-rails-assets'
  gem 'guard-jasmine-headless-webkit'
jasmine-headless-webkit
jasmine-headless-webkit
It integrates with Guard for a continuous testing setup.
It compiles CoffeeScript, both for your tests and for your application
logic.
It can be con鍖gured like RSpec, and its output is very similar to RSpec's
output.
It provides cleaner debugging and backtrace output than a lot of other
console-based test tools provide.
It's fast.
jasmine-headless-webkit


Needs Qt (actually this is not a problem but Qt compiles a lot of time)
-
jasmine-jquery
jasmine-jquery

Ads JQuery speci鍖c Matchers

Ads Fixtures

Ads Event Spies
jasmine-jquery Matchers

toBe selector    toContain selector
toBeChecked      toExist
toBeEmpty        toHaveAttr name, value
toBeHidden       toHaveClass className
toBeSelected     toHandle eventName
toBeVisible      etc...
jasmine-jquery Fixtures
<div id="my-鍖xture">some complex content here</div>



loadFixtures 'my鍖xture.html'
$('#my-鍖xture').myTestedPlugin()
expect($('#my-鍖xture')).to...
jasmine-jquery Event Spies


spyOnEvent $('#some_element'), 'click'
$('#some_element').click()
expect('click').toHaveBeenTriggeredOn $'#some_element'
jasmine-ajax
jasmine-ajax


1.De鍖ning test responses
2.Installing the mock
3.De鍖ning the response for each request
4.Inspecting Ajax requests and setting expectations on them
Time for code
Links

https://github.com/pivotal/jasmine/wiki
http://jashkenas.github.com/coffee-script/
http://johnbintz.github.com/jasmine-headless-webkit/
https://github.com/velesin/jasmine-jquery
https://github.com/pivotal/jasmine-ajax
https://github.com/guard/guard/wiki/List-of-available-Guards

More Related Content

Testing JS with Jasmine

Editor's Notes