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,15 +15,18 @@ 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:
{% highlight javascript %}
it('should be a test', function () {
var foo = 0
foo++;
expect(foo).toEqual(1);
});
{% endhighlight %}
and
{% highlight javascript %}
it('should be a test', function () {
runs( function () {
var foo = 0
@ -32,9 +35,11 @@ and
expect(foo).toEqual(1);
});
});
{% endhighlight %}
Multiple `runs()` blocks in a spec will run serially. For example,
{% highlight javascript %}
it('should be a test', function () {
runs( function () {
var foo = 0
@ -49,9 +54,11 @@ 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!
{% highlight javascript %}
it('should be a test', function () {
runs( function () {
this.foo = 0
@ -71,12 +78,14 @@ Multiple `runs()` blocks in a spec will run serially. For example,
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:
{% highlight javascript %}
it('should be a test', function () {
runs(function () {
this.foo = 0;
@ -96,6 +105,7 @@ timeout before the next block is run. You supply a time to wait before the next
expect(this.foo).toEqual(1);
});
});
{% endhighlight %}
What's happening here?
@ -113,6 +123,7 @@ 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:
{% highlight javascript %}
describe('Spreadsheet', function() {
it('should calculate the total asynchronously', function () {
var spreadsheet = new Spreadsheet();
@ -128,6 +139,7 @@ time, or you may specify a maxiumum period in milliseconds before timing out:
});
});
});
{% 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:
{% highlight javascript %}
describe('some suite', function () {
var suiteWideFoo;
beforeEach(function () {
@ -19,9 +19,11 @@ A suite can have a `beforeEach()` declaration. It takes a function that is run b
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:
{% highlight javascript %}
var runnerWideFoo = [];
beforeEach(function () {
@ -29,7 +31,6 @@ A runner can also have `beforeEach()` declarations. Runner `beforeEach()` functi
});
describe('some suite', function () {
beforeEach(function () {
runnerWideFoo.push('suite');
});
@ -38,13 +39,14 @@ A runner can also have `beforeEach()` declarations. Runner `beforeEach()` functi
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:
{% highlight javascript %}
describe('some suite', function () {
var suiteWideFoo;
afterEach(function () {
suiteWideFoo = 0;
@ -58,9 +60,11 @@ Similarly, there is an `afterEach()` declaration. It takes a function that is r
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:
{% highlight javascript %}
var runnerWideFoo = [];
afterEach(function () {
@ -68,7 +72,6 @@ A runner can also have an `afterEach()` declarations. Runner `afterEach()` funct
});
describe('some suite', function () {
afterEach(function () {
runnerWideFoo.push('suite');
});
@ -81,11 +84,13 @@ A runner can also have an `afterEach()` declarations. Runner `afterEach()` funct
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.
{% highlight javascript %}
describe('some suite', function () {
it(function () {
var originalTitle = window.title;
@ -93,4 +98,4 @@ A spec may ask Jasmine to execute some code after the spec has finished running;
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

@ -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()`:
{% 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:
{% highlight javascript %}
beforeEach(function() {
this.addMatchers({
toBeVisible: function() { return this.actual.isVisible(); }
});
});
{% endhighlight %}

View File

@ -9,6 +9,7 @@ Jasmine integrates 'spies' that permit many spying, mocking, and faking behavior
Here are a few examples:
{% highlight javascript %}
var Klass = function () {
};
@ -53,9 +54,11 @@ 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.
{% highlight javascript %}
var Klass = function () {
};
@ -77,6 +80,7 @@ Spies can be very useful for testing AJAX or other asynchronous behaviors that t
expect(callback).toHaveBeenCalledWith(someResponseData);
});
{% endhighlight %}
There are spy-specfic matchers that are very handy.