#Due Monday
P 5-9 Due Monday along with P10 MVP Outline as Issue On Challenge P10 Presented on Friday
P7 Reviews
#Jasmine - Unit Testing for JavaScript
#Headline
- Demonstrate Jasmine for testing JavaScript
#Overview
- Jasmine for Simple MVC
- Considerations for Testing Components
- Using Spies (a.k.a. mocks, doubles)
- Optional: Using Karma (Testacular) to run JavaScript Unit Tests
#Jasmine for Simple MVC
- Unit testing Model
cat_simple_mvc/test/spec/CatModelSpec.js
beforeEach
it
describe
#Considerations for Testing Components
- Loose binding
- Pass in Objects to your constructors / factories
#Using Spies (a.k.a. mocks, doubles)
- Some object rely on other
- Unit testing Controller
cat_simple_mvc/test/spec/CatControllerSpec.js
#Review
- Jasmine for Simple MVC
- Considerations for Testing Components
- Using Spies (a.k.a. mocks, doubles)
#Optional Bonus
- Leave if you want
- Karma node based javascript test running
#Using Karma to run JavaScript Unit Tests
- From the angular team
- Used to be called Testacular renamed for unknown reason
karma start
- Launches browser
- Watches Filesystem
- Runs tests on changes
ddescribe
- run single suiteiit
- run single testconsole.log('xyz');
debugger;
- Multi Browser Support
- Just connect another browser to http://localhost/
- Any browser that can connect
- Code Coverage? - Go Explore
##Installation
- Requires
node
$ node -v
# v0.10.29
$ brew install npm
- Install Karma Packages
- Global install may require sudo
- npm-global-without-sudo-linux.md
$ cd my_project
$ npm install -g karma-cli
$ npm install -g phantomjs
$ npm install karma-jasmine@2_0 --save-dev
$ npm install karma-phantomjs-launcher --save-dev
- I'm using Jasmine 2.0
- https://github.com/karma-runner/karma-jasmine#jasmine-20-docs
$ karma init karma.conf.js
-
Use Chrome
-
Use
PhantomJS
instead -
--
browsers: ['Chrome'],
-- -
browsers: ['PhantomJS'],
-
Coverage
npm install karma-coverage --save-dev
-
Adding View Tests
-
Use
karma-jasmine-jquery
for fixtures -
Requires jquery on tests :(
-
cat_simple_mvc/test/spec/CatViewSpec.js
#Resources
- Jasmine 2.0
- Pragmatic Javascript Testing with Jasmine/
- Jasmine Spy Cheatsheet
- http://karma-runner.github.io/0.12/index.html
- Jasmine and Karma
- 2012: Testacular, the Spectacular JavaScript Test Runner - Vojta Jina
#More Links
- https://github.com/pivotal/jasmine-gem
- http://johnbintz.github.io/jasmine-headless-webkit/
- http://tobyho.com/2011/12/15/jasmine-spy-cheatsheet/
- http://eclipsesource.com/blogs/2014/03/27/mocks-in-jasmine-tests/
- http://www.htmlgoodies.com/html5/javascript/spy-on-javascript-methods-using-
- https://github.com/velesin/jasmine-jquery
- https://github.com/pivotal/jasmine-ajax
Thank you!