Add js syntax highlighting.
This commit is contained in:
parent
c04584ad0f
commit
602d8e4c47
@ -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:
|
`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 () {
|
it('should be a test', function () {
|
||||||
var foo = 0
|
var foo = 0
|
||||||
foo++;
|
foo++;
|
||||||
|
|
||||||
expect(foo).toEqual(1);
|
expect(foo).toEqual(1);
|
||||||
});
|
});
|
||||||
|
{% endhighlight %}
|
||||||
|
|
||||||
and
|
and
|
||||||
|
|
||||||
|
{% highlight javascript %}
|
||||||
it('should be a test', function () {
|
it('should be a test', function () {
|
||||||
runs( function () {
|
runs( function () {
|
||||||
var foo = 0
|
var foo = 0
|
||||||
@ -32,9 +35,11 @@ and
|
|||||||
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,
|
||||||
|
|
||||||
|
{% highlight javascript %}
|
||||||
it('should be a test', function () {
|
it('should be a test', function () {
|
||||||
runs( function () {
|
runs( function () {
|
||||||
var foo = 0
|
var foo = 0
|
||||||
@ -49,9 +54,11 @@ Multiple `runs()` blocks in a spec will run serially. For example,
|
|||||||
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!
|
||||||
|
|
||||||
|
{% highlight javascript %}
|
||||||
it('should be a test', function () {
|
it('should be a test', function () {
|
||||||
runs( function () {
|
runs( function () {
|
||||||
this.foo = 0
|
this.foo = 0
|
||||||
@ -71,12 +78,14 @@ Multiple `runs()` blocks in a spec will run serially. For example,
|
|||||||
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:
|
||||||
|
|
||||||
|
{% highlight javascript %}
|
||||||
it('should be a test', function () {
|
it('should be a test', function () {
|
||||||
runs(function () {
|
runs(function () {
|
||||||
this.foo = 0;
|
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);
|
expect(this.foo).toEqual(1);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
{% endhighlight %}
|
||||||
|
|
||||||
What's happening here?
|
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
|
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:
|
||||||
|
|
||||||
|
{% highlight javascript %}
|
||||||
describe('Spreadsheet', function() {
|
describe('Spreadsheet', function() {
|
||||||
it('should calculate the total asynchronously', function () {
|
it('should calculate the total asynchronously', function () {
|
||||||
var spreadsheet = new Spreadsheet();
|
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
|
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
|
||||||
|
@ -7,8 +7,8 @@
|
|||||||
|
|
||||||
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:
|
||||||
|
|
||||||
|
{% highlight javascript %}
|
||||||
describe('some suite', function () {
|
describe('some suite', function () {
|
||||||
|
|
||||||
var suiteWideFoo;
|
var suiteWideFoo;
|
||||||
|
|
||||||
beforeEach(function () {
|
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);
|
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:
|
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 = [];
|
var runnerWideFoo = [];
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
@ -29,7 +31,6 @@ A runner can also have `beforeEach()` declarations. Runner `beforeEach()` functi
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('some suite', function () {
|
describe('some suite', function () {
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
runnerWideFoo.push('suite');
|
runnerWideFoo.push('suite');
|
||||||
});
|
});
|
||||||
@ -38,13 +39,14 @@ A runner can also have `beforeEach()` declarations. Runner `beforeEach()` functi
|
|||||||
expect(runnerWideFoo).toEqual(['runner', 'suite']);
|
expect(runnerWideFoo).toEqual(['runner', 'suite']);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
{% endhighlight %}
|
||||||
|
|
||||||
#### 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:
|
||||||
|
|
||||||
|
{% highlight javascript %}
|
||||||
describe('some suite', function () {
|
describe('some suite', function () {
|
||||||
|
|
||||||
var suiteWideFoo;
|
var suiteWideFoo;
|
||||||
afterEach(function () {
|
afterEach(function () {
|
||||||
suiteWideFoo = 0;
|
suiteWideFoo = 0;
|
||||||
@ -58,9 +60,11 @@ Similarly, there is an `afterEach()` declaration. It takes a function that is r
|
|||||||
expect(suiteWideFoo).toEqual(0);
|
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:
|
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 = [];
|
var runnerWideFoo = [];
|
||||||
|
|
||||||
afterEach(function () {
|
afterEach(function () {
|
||||||
@ -68,7 +72,6 @@ A runner can also have an `afterEach()` declarations. Runner `afterEach()` funct
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('some suite', function () {
|
describe('some suite', function () {
|
||||||
|
|
||||||
afterEach(function () {
|
afterEach(function () {
|
||||||
runnerWideFoo.push('suite');
|
runnerWideFoo.push('suite');
|
||||||
});
|
});
|
||||||
@ -81,11 +84,13 @@ A runner can also have an `afterEach()` declarations. Runner `afterEach()` funct
|
|||||||
expect(runnerWideFoo).toEqual(['suite', 'runner']);
|
expect(runnerWideFoo).toEqual(['suite', 'runner']);
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
{% endhighlight %}
|
||||||
|
|
||||||
### 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.
|
||||||
|
|
||||||
|
{% highlight javascript %}
|
||||||
describe('some suite', function () {
|
describe('some suite', function () {
|
||||||
it(function () {
|
it(function () {
|
||||||
var originalTitle = window.title;
|
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");
|
MyWindow.setTitle("new value");
|
||||||
expect(window.title).toEqual("new value");
|
expect(window.title).toEqual("new value");
|
||||||
});
|
});
|
||||||
|
{% endhighlight %}
|
||||||
|
@ -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;
|
||||||
|
@ -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()`:
|
||||||
|
|
||||||
|
{% highlight javascript %}
|
||||||
toBeLessThan: function(expected) {
|
toBeLessThan: function(expected) {
|
||||||
return this.actual < 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:
|
||||||
|
|
||||||
|
{% highlight javascript %}
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
this.addMatchers({
|
this.addMatchers({
|
||||||
toBeVisible: function() { return this.actual.isVisible(); }
|
toBeVisible: function() { return this.actual.isVisible(); }
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
{% endhighlight %}
|
||||||
|
@ -9,6 +9,7 @@ Jasmine integrates 'spies' that permit many spying, mocking, and faking behavior
|
|||||||
|
|
||||||
Here are a few examples:
|
Here are a few examples:
|
||||||
|
|
||||||
|
{% highlight javascript %}
|
||||||
var Klass = function () {
|
var Klass = function () {
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -53,9 +54,11 @@ Here are a few examples:
|
|||||||
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.
|
||||||
|
|
||||||
|
{% highlight javascript %}
|
||||||
var Klass = function () {
|
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);
|
expect(callback).toHaveBeenCalledWith(someResponseData);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
{% endhighlight %}
|
||||||
|
|
||||||
There are spy-specfic matchers that are very handy.
|
There are spy-specfic matchers that are very handy.
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user