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,27 +15,32 @@ 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:
it('should be a test', function () {
{% highlight javascript %}
it('should be a test', function () {
var foo = 0
foo++;
expect(foo).toEqual(1);
});
});
{% endhighlight %}
and
it('should be a test', function () {
{% highlight javascript %}
it('should be a test', function () {
runs( function () {
var foo = 0
foo++;
expect(foo).toEqual(1);
});
});
});
{% endhighlight %}
Multiple `runs()` blocks in a spec will run serially. For example,
it('should be a test', function () {
{% highlight javascript %}
it('should be a test', function () {
runs( function () {
var foo = 0
foo++;
@ -48,11 +53,13 @@ Multiple `runs()` blocks in a spec will run serially. For example,
expect(bar).toEqual(1);
});
});
});
{% endhighlight %}
`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 %}
it('should be a test', function () {
runs( function () {
this.foo = 0
this.foo++;
@ -70,14 +77,16 @@ Multiple `runs()` blocks in a spec will run serially. For example,
expect(foo).toEqual(2);
expect(bar).toEqual(1);
});
});
});
{% endhighlight %}
#### `waits(timeout)`
`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:
it('should be a test', function () {
{% highlight javascript %}
it('should be a test', function () {
runs(function () {
this.foo = 0;
var that = this;
@ -95,7 +104,8 @@ timeout before the next block is run. You supply a time to wait before the next
runs(function () {
expect(this.foo).toEqual(1);
});
});
});
{% endhighlight %}
What's happening here?
@ -113,7 +123,8 @@ 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
time, or you may specify a maxiumum period in milliseconds before timing out:
describe('Spreadsheet', function() {
{% highlight javascript %}
describe('Spreadsheet', function() {
it('should calculate the total asynchronously', function () {
var spreadsheet = new Spreadsheet();
spreadsheet.fillWith(lotsOfFixureDataValues());
@ -127,7 +138,8 @@ time, or you may specify a maxiumum period in milliseconds before timing out:
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
its total, which presumably is a slow operation and therefore happens asynchronously. We ask Jasmine to wait until the

View File

@ -7,8 +7,8 @@
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;
beforeEach(function () {
@ -18,18 +18,19 @@ A suite can have a `beforeEach()` declaration. It takes a function that is run b
it('should equal bar', function () {
expect(suiteWideFoo).toEqual(1);
});
});
});
{% endhighlight %}
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');
});
describe('some suite', function () {
});
describe('some suite', function () {
beforeEach(function () {
runnerWideFoo.push('suite');
});
@ -37,14 +38,15 @@ A runner can also have `beforeEach()` declarations. Runner `beforeEach()` functi
it('should equal bar', function () {
expect(runnerWideFoo).toEqual(['runner', 'suite']);
});
});
});
{% endhighlight %}
#### afterEach
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;
@ -57,18 +59,19 @@ Similarly, there is an `afterEach()` declaration. It takes a function that is r
it('should equal 0 after', function () {
expect(suiteWideFoo).toEqual(0);
};
});
});
{% endhighlight %}
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');
});
describe('some suite', function () {
});
describe('some suite', function () {
afterEach(function () {
runnerWideFoo.push('suite');
});
@ -80,17 +83,19 @@ A runner can also have an `afterEach()` declarations. Runner `afterEach()` funct
it('should be populated after', function () {
expect(runnerWideFoo).toEqual(['suite', 'runner']);
};
});
});
{% endhighlight %}
### 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.
describe('some suite', function () {
{% highlight javascript %}
describe('some suite', function () {
it(function () {
var originalTitle = window.title;
this.after(function() { window.title = originalTitle; });
MyWindow.setTitle("new value");
expect(window.title).toEqual("new value");
});
{% endhighlight %}

View File

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

View File

@ -19,9 +19,9 @@ describe("Jasmine", function() {
## Adding Jasmine to your Rails project
{% highlight sh %}
$ gem install jasmine
$ script/generate jasmine
$ rake spec
$ gem install jasmine
$ script/generate jasmine
$ rake spec
{% endhighlight %}
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()`:
toBeLessThan: function(expected) {
{% highlight javascript %}
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:
beforeEach(function() {
{% highlight javascript %}
beforeEach(function() {
this.addMatchers({
toBeVisible: function() { return this.actual.isVisible(); }
});
});
});
{% endhighlight %}

View File

@ -9,24 +9,25 @@ Jasmine integrates 'spies' that permit many spying, mocking, and faking behavior
Here are a few examples:
var Klass = function () {
};
{% highlight javascript %}
var Klass = function () {
};
Klass.staticMethod = function (arg) {
Klass.staticMethod = function (arg) {
return arg;
};
};
Klass.prototype.method = function (arg) {
Klass.prototype.method = function (arg) {
return arg;
};
};
Klass.prototype.methodWithCallback = function (callback) {
Klass.prototype.methodWithCallback = function (callback) {
return callback('foo');
};
};
...
...
describe("spy behavior", function() {
describe("spy behavior", function() {
it('should spy on a static method of Klass', function() {
spyOn(Klass, 'staticMethod');
Klass.staticMethod('foo argument');
@ -52,20 +53,22 @@ Here are a few examples:
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.
var Klass = function () {
};
{% highlight javascript %}
var Klass = function () {
};
var Klass.prototype.asyncMethod = function (callback) {
var Klass.prototype.asyncMethod = function (callback) {
someAsyncCall(callback);
};
};
...
...
it('should test async call') {
it('should test async call') {
spyOn(Klass, 'asyncMethod');
var callback = jasmine.createSpy();
@ -76,7 +79,8 @@ Spies can be very useful for testing AJAX or other asynchronous behaviors that t
Klass.asyncMethod.mostRecentCall.args[0](someResponseData);
expect(callback).toHaveBeenCalledWith(someResponseData);
});
});
{% endhighlight %}
There are spy-specfic matchers that are very handy.