Rquire.js with Jasmine 2
Require
Project structure
Project/ │ ├── css/ │ └── jasmine.css ├── js/ │ ├── require.js │ ├── main.js │ ├── models/ │ │ ├── player.js │ │ └── song.js │ └── test │ ├── spec/ │ │ └── playerSpec.js │ ├── jasmine.js │ ├── jasmine-html.js │ └── boot.js └── index.html
index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title></title> <link rel="stylesheet" href="css/jasmine.css"> </head> <body> <!-- development --> <script src="js/test/jasmine.js"></script> <script src="js/test/jasmine-html.js"></script> <script src="js/test/boot.js"></script> <!-- require.js --> <script src="js/require.js" data-main="js/main"></script> </body> </html> |
main.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
require.config({ baseUrl: "/", urlArgs: 'cb=' + Math.random(), paths: { // jasmine 'jasmine': 'js/test/jasmine', 'jasmine-html': 'js/test/jasmine-html', 'jasmine-boot': 'js/test/boot', // models 'models': 'js/models', // testing files 'spec': 'js/test/spec', }, shim: { 'jasmine': { exports: 'jasmine' }, 'jasmine-html': { deps: ['jasmine'], exports: 'jasmine' }, 'jasmine-boot': { deps: ['jasmine', 'jasmine-html'], exports: 'jasmine' } } }); require(['jasmine-boot'], function() { // your models... require(['models/player', 'models/song'], function() { // loading spec files require(['spec/playerSpec'], function() { // Initialize the HTML Reporter and execute the environment (setup by 'boot.js') window.onload(); }); }); }); |
js/models/player.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
function Player() { } Player.prototype.play = function(song) { this.currentlyPlayingSong = song; this.isPlaying = true; }; Player.prototype.pause = function() { this.isPlaying = false; }; Player.prototype.resume = function() { if (this.isPlaying) { throw new Error("song is already playing"); } this.isPlaying = true; }; Player.prototype.makeFavorite = function() { this.currentlyPlayingSong.persistFavoriteStatus(true); }; |
js/models/song.js
1 2 3 4 5 6 7 |
function Song() { } Song.prototype.persistFavoriteStatus = function(value) { // something complicated throw new Error("not yet implemented"); }; |
js/test/spec/playerSpec.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
define('playerSpec', function(){ describe("Player", function() { var player; var song; beforeEach(function() { player = new Player(); song = new Song(); }); it("should be able to play a Song", function() { player.play(song); expect(player.currentlyPlayingSong).toEqual(song); //demonstrates use of custom matcher expect(player).toBePlaying(song); }); describe("when song has been paused", function() { beforeEach(function() { player.play(song); player.pause(); }); it("should indicate that the song is currently paused", function() { expect(player.isPlaying).toBeFalsy(); // demonstrates use of 'not' with a custom matcher expect(player).not.toBePlaying(song); }); it("should be possible to resume", function() { player.resume(); expect(player.isPlaying).toBeTruthy(); expect(player.currentlyPlayingSong).toEqual(song); }); }); // demonstrates use of spies to intercept and test method calls it("tells the current song if the user has made it a favorite", function() { spyOn(song, 'persistFavoriteStatus'); player.play(song); player.makeFavorite(); expect(song.persistFavoriteStatus).toHaveBeenCalledWith(true); }); //demonstrates use of expected exceptions describe("#resume", function() { it("should throw an exception if song is already playing", function() { player.play(song); expect(function() { player.resume(); }).toThrowError("song is already playing"); }); }); }); }); |
method
- toBe()
- toEqual()
- .not
- toMatch(/Regex/)
- toBeDefined()
- toBeUndefined()
- toBeNull()
- toBeTruthy()
- toBeFalsy()
- toContain()
- toBeLessThan()
- toBeGreaterThan()
- toBeCloseTo(e, number)
- toThrow()
- spyOn(obj, ‘method’)
- toHaveBeenCalled()
- toHaveBeenCalledWith(args…)
- mostRecentCall