Add js syntax highlighting.

This commit is contained in:
Christian Williams 2010-09-22 00:25:34 -07:00
parent c04584ad0f
commit 602d8e4c47
6 changed files with 218 additions and 189 deletions

View File

@ -15,87 +15,97 @@ Jasmine allows you to do this with `runs()`, `waits()` and `waitsFor()` blocks.
`runs()` blocks by themselves simply run as if they were called directly. The following snippets of code provide similar results: `runs()` blocks by themselves simply run as if they were called directly. The following snippets of code provide similar results:
it('should be a test', function () { {% highlight javascript %}
var foo = 0 it('should be a test', function () {
foo++; var foo = 0
foo++;
expect(foo).toEqual(1); expect(foo).toEqual(1);
}); });
{% endhighlight %}
and and
it('should be a test', function () { {% highlight javascript %}
runs( function () { it('should be a test', function () {
var foo = 0 runs( function () {
foo++; var foo = 0
foo++;
expect(foo).toEqual(1); expect(foo).toEqual(1);
}); });
}); });
{% endhighlight %}
Multiple `runs()` blocks in a spec will run serially. For example, Multiple `runs()` blocks in a spec will run serially. For example,
it('should be a test', function () { {% highlight javascript %}
runs( function () { it('should be a test', function () {
var foo = 0 runs( function () {
foo++; var foo = 0
foo++;
expect(foo).toEqual(1); expect(foo).toEqual(1);
}); });
runs( function () { runs( function () {
var bar = 0 var bar = 0
bar++; bar++;
expect(bar).toEqual(1); expect(bar).toEqual(1);
}); });
}); });
{% endhighlight %}
`runs()` blocks share functional scope -- `this` properties will be common to all blocks, but declared `var`'s will not! `runs()` blocks share functional scope -- `this` properties will be common to all blocks, but declared `var`'s will not!
it('should be a test', function () { {% highlight javascript %}
runs( function () { it('should be a test', function () {
this.foo = 0 runs( function () {
this.foo++; this.foo = 0
var bar = 0; this.foo++;
bar++; var bar = 0;
bar++;
expect(this.foo).toEqual(1); expect(this.foo).toEqual(1);
expect(bar).toEqual(1); expect(bar).toEqual(1);
}); });
runs( function () { runs( function () {
this.foo++; this.foo++;
var bar = 0 var bar = 0
bar++; bar++;
expect(foo).toEqual(2); expect(foo).toEqual(2);
expect(bar).toEqual(1); expect(bar).toEqual(1);
}); });
}); });
{% endhighlight %}
#### `waits(timeout)` #### `waits(timeout)`
`runs()` blocks exist so you can test asynchronous processes. The function `waits()` works with `runs()` to provide a naive `runs()` blocks exist so you can test asynchronous processes. The function `waits()` works with `runs()` to provide a naive
timeout before the next block is run. You supply a time to wait before the next `runs()` function is executed. For example: timeout before the next block is run. You supply a time to wait before the next `runs()` function is executed. For example:
it('should be a test', function () { {% highlight javascript %}
runs(function () { it('should be a test', function () {
this.foo = 0; runs(function () {
var that = this; this.foo = 0;
setTimeout(function () { var that = this;
that.foo++; setTimeout(function () {
}, 250); that.foo++;
}); }, 250);
});
runs(function () { runs(function () {
expect(this.foo).toEqual(0); expect(this.foo).toEqual(0);
}); });
waits(500); waits(500);
runs(function () { runs(function () {
expect(this.foo).toEqual(1); expect(this.foo).toEqual(1);
}); });
}); });
{% endhighlight %}
What's happening here? What's happening here?
@ -113,21 +123,23 @@ some other operation. But what if you don't know exactly how long you need to wa
the provided function returns `true` before continuing with the next block. This may mean waiting an arbitrary period of the provided function returns `true` before continuing with the next block. This may mean waiting an arbitrary period of
time, or you may specify a maxiumum period in milliseconds before timing out: time, or you may specify a maxiumum period in milliseconds before timing out:
describe('Spreadsheet', function() { {% highlight javascript %}
it('should calculate the total asynchronously', function () { describe('Spreadsheet', function() {
var spreadsheet = new Spreadsheet(); it('should calculate the total asynchronously', function () {
spreadsheet.fillWith(lotsOfFixureDataValues()); var spreadsheet = new Spreadsheet();
spreadsheet.asynchronouslyCalculateTotal(); spreadsheet.fillWith(lotsOfFixureDataValues());
spreadsheet.asynchronouslyCalculateTotal();
waitsFor(function() { waitsFor(function() {
return spreadsheet.calculationIsComplete(); return spreadsheet.calculationIsComplete();
}, "Spreadsheet calculation never completed", 10000); }, "Spreadsheet calculation never completed", 10000);
runs(function () { runs(function () {
expect(spreadsheet.total).toEqual(123456); expect(spreadsheet.total).toEqual(123456);
});
});
}); });
});
});
{% endhighlight %}
In this example, we create a spreadsheet and fill it with some sample data. We then ask the spreadsheet to start calculating In this example, we create a spreadsheet and fill it with some sample data. We then ask the spreadsheet to start calculating
its total, which presumably is a slow operation and therefore happens asynchronously. We ask Jasmine to wait until the its total, which presumably is a slow operation and therefore happens asynchronously. We ask Jasmine to wait until the

View File

@ -7,90 +7,95 @@
A suite can have a `beforeEach()` declaration. It takes a function that is run before each spec. For example: A suite can have a `beforeEach()` declaration. It takes a function that is run before each spec. For example:
describe('some suite', function () { {% highlight javascript %}
describe('some suite', function () {
var suiteWideFoo;
var suiteWideFoo; beforeEach(function () {
suiteWideFoo = 1;
});
beforeEach(function () { it('should equal bar', function () {
suiteWideFoo = 1; expect(suiteWideFoo).toEqual(1);
}); });
});
it('should equal bar', function () { {% endhighlight %}
expect(suiteWideFoo).toEqual(1);
});
});
A runner can also have `beforeEach()` declarations. Runner `beforeEach()` functions are executed before every spec in all suites, and execute BEFORE suite `beforeEach()` functions. For example: A runner can also have `beforeEach()` declarations. Runner `beforeEach()` functions are executed before every spec in all suites, and execute BEFORE suite `beforeEach()` functions. For example:
var runnerWideFoo = []; {% highlight javascript %}
var runnerWideFoo = [];
beforeEach(function () { beforeEach(function () {
runnerWideFoo.push('runner'); runnerWideFoo.push('runner');
}); });
describe('some suite', function () { describe('some suite', function () {
beforeEach(function () {
runnerWideFoo.push('suite');
});
beforeEach(function () { it('should equal bar', function () {
runnerWideFoo.push('suite'); expect(runnerWideFoo).toEqual(['runner', 'suite']);
}); });
});
it('should equal bar', function () { {% endhighlight %}
expect(runnerWideFoo).toEqual(['runner', 'suite']);
});
});
#### afterEach #### afterEach
Similarly, there is an `afterEach()` declaration. It takes a function that is run after each spec. For example: Similarly, there is an `afterEach()` declaration. It takes a function that is run after each spec. For example:
describe('some suite', function () { {% highlight javascript %}
describe('some suite', function () {
var suiteWideFoo;
afterEach(function () {
suiteWideFoo = 0;
});
var suiteWideFoo; it('should equal 1', function () {
afterEach(function () { expect(suiteWideFoo).toEqual(1);
suiteWideFoo = 0; });
});
it('should equal 1', function () { it('should equal 0 after', function () {
expect(suiteWideFoo).toEqual(1); expect(suiteWideFoo).toEqual(0);
}); };
});
it('should equal 0 after', function () { {% endhighlight %}
expect(suiteWideFoo).toEqual(0);
};
});
A runner can also have an `afterEach()` declarations. Runner `afterEach()` functions are executed after every spec in all suites, and execute AFTER suite `afterEach()` functions. For example: A runner can also have an `afterEach()` declarations. Runner `afterEach()` functions are executed after every spec in all suites, and execute AFTER suite `afterEach()` functions. For example:
var runnerWideFoo = []; {% highlight javascript %}
var runnerWideFoo = [];
afterEach(function () { afterEach(function () {
runnerWideFoo.push('runner'); runnerWideFoo.push('runner');
}); });
describe('some suite', function () { describe('some suite', function () {
afterEach(function () {
runnerWideFoo.push('suite');
});
afterEach(function () { it('should be empty', function () {
runnerWideFoo.push('suite'); expect(runnerWideFoo).toEqual([]);
}); });
it('should be empty', function () { it('should be populated after', function () {
expect(runnerWideFoo).toEqual([]); expect(runnerWideFoo).toEqual(['suite', 'runner']);
}); };
});
it('should be populated after', function () { {% endhighlight %}
expect(runnerWideFoo).toEqual(['suite', 'runner']);
};
});
### Single-spec After functions ### Single-spec After functions
A spec may ask Jasmine to execute some code after the spec has finished running; the code will run whether the spec finishes successfully or not. Multiple after functions may be given. A spec may ask Jasmine to execute some code after the spec has finished running; the code will run whether the spec finishes successfully or not. Multiple after functions may be given.
describe('some suite', function () { {% highlight javascript %}
it(function () { describe('some suite', function () {
var originalTitle = window.title; it(function () {
this.after(function() { window.title = originalTitle; }); var originalTitle = window.title;
MyWindow.setTitle("new value"); this.after(function() { window.title = originalTitle; });
expect(window.title).toEqual("new value"); MyWindow.setTitle("new value");
}); expect(window.title).toEqual("new value");
});
{% endhighlight %}

View File

@ -81,6 +81,11 @@ pre,code
/* font-size: 1.142em; */ /* font-size: 1.142em; */
} }
pre {
border-left: 2px solid #55862e;
padding-left: 1em;
}
.noul .noul
{ {
border-bottom: 0; border-bottom: 0;

View File

@ -19,9 +19,9 @@ describe("Jasmine", function() {
## Adding Jasmine to your Rails project ## Adding Jasmine to your Rails project
{% highlight sh %} {% highlight sh %}
$ gem install jasmine $ gem install jasmine
$ script/generate jasmine $ script/generate jasmine
$ rake spec $ rake spec
{% endhighlight %} {% endhighlight %}
Jasmine can be run by on a static web page, on your Continuous Integration environment, or with [node.js](http://nodejs.org). Jasmine can be run by on a static web page, on your Continuous Integration environment, or with [node.js](http://nodejs.org).

View File

@ -43,15 +43,18 @@ It's extremely easy to create new matchers for your app. A matcher function rece
Here's the definition of `toBeLessThan()`: Here's the definition of `toBeLessThan()`:
toBeLessThan: function(expected) { {% highlight javascript %}
return this.actual < expected; toBeLessThan: function(expected) {
}; return this.actual < expected;
};
{% endhighlight %}
To add the matcher to your suite, call `this.addMatchers()` from within a `before` or `it` block. Call it with an object mapping matcher name to function: To add the matcher to your suite, call `this.addMatchers()` from within a `before` or `it` block. Call it with an object mapping matcher name to function:
beforeEach(function() { {% highlight javascript %}
this.addMatchers({ beforeEach(function() {
toBeVisible: function() { return this.actual.isVisible(); } this.addMatchers({
}); toBeVisible: function() { return this.actual.isVisible(); }
}); });
});
{% endhighlight %}

View File

@ -9,74 +9,78 @@ Jasmine integrates 'spies' that permit many spying, mocking, and faking behavior
Here are a few examples: Here are a few examples:
var Klass = function () { {% highlight javascript %}
}; var Klass = function () {
};
Klass.staticMethod = function (arg) { Klass.staticMethod = function (arg) {
return arg; return arg;
}; };
Klass.prototype.method = function (arg) { Klass.prototype.method = function (arg) {
return arg; return arg;
}; };
Klass.prototype.methodWithCallback = function (callback) { Klass.prototype.methodWithCallback = function (callback) {
return callback('foo'); return callback('foo');
}; };
... ...
describe("spy behavior", function() { describe("spy behavior", function() {
it('should spy on a static method of Klass', function() { it('should spy on a static method of Klass', function() {
spyOn(Klass, 'staticMethod'); spyOn(Klass, 'staticMethod');
Klass.staticMethod('foo argument'); Klass.staticMethod('foo argument');
expect(Klass.staticMethod).toHaveBeenCalledWith('foo argument'); expect(Klass.staticMethod).toHaveBeenCalledWith('foo argument');
}); });
it('should spy on an instance method of a Klass', function() { it('should spy on an instance method of a Klass', function() {
var obj = new Klass(); var obj = new Klass();
spyOn(obj, 'method'); spyOn(obj, 'method');
obj.method('foo argument'); obj.method('foo argument');
expect(obj.method).toHaveBeenCalledWith('foo argument'); expect(obj.method).toHaveBeenCalledWith('foo argument');
var obj2 = new Klass(); var obj2 = new Klass();
spyOn(obj2, 'method'); spyOn(obj2, 'method');
expect(obj2.method).not.toHaveBeenCalled(); expect(obj2.method).not.toHaveBeenCalled();
}); });
it('should spy on Klass#methodWithCallback', function() { it('should spy on Klass#methodWithCallback', function() {
var callback = jasmine.createSpy(); var callback = jasmine.createSpy();
new Klass().methodWithCallback(callback); new Klass().methodWithCallback(callback);
expect(callback).toHaveBeenCalledWith('foo'); expect(callback).toHaveBeenCalledWith('foo');
}); });
}); });
{% endhighlight %}
Spies can be very useful for testing AJAX or other asynchronous behaviors that take callbacks by faking the method firing an async call. Spies can be very useful for testing AJAX or other asynchronous behaviors that take callbacks by faking the method firing an async call.
var Klass = function () { {% highlight javascript %}
}; var Klass = function () {
};
var Klass.prototype.asyncMethod = function (callback) { var Klass.prototype.asyncMethod = function (callback) {
someAsyncCall(callback); someAsyncCall(callback);
}; };
... ...
it('should test async call') { it('should test async call') {
spyOn(Klass, 'asyncMethod'); spyOn(Klass, 'asyncMethod');
var callback = jasmine.createSpy(); var callback = jasmine.createSpy();
Klass.asyncMethod(callback); Klass.asyncMethod(callback);
expect(callback).not.toHaveBeenCalled(); expect(callback).not.toHaveBeenCalled();
var someResponseData = 'foo'; var someResponseData = 'foo';
Klass.asyncMethod.mostRecentCall.args[0](someResponseData); Klass.asyncMethod.mostRecentCall.args[0](someResponseData);
expect(callback).toHaveBeenCalledWith(someResponseData); expect(callback).toHaveBeenCalledWith(someResponseData);
}); });
{% endhighlight %}
There are spy-specfic matchers that are very handy. There are spy-specfic matchers that are very handy.