Bryan Albrecht

Jasmine

In this Tutorial I'm going to explain what Jasmine is.

Benefit

With jasmine it is possible to test Javascript.

In this method the tests are written, before the code.

This is the TestFirst principe.

Realization

To be able to use jasmine, first you have to download the libraries.

You can download them on the following page:

Link

After you have downloaded the libraries you have to move the file SpecRunner.html and the folders lib, spec and src in the project folder.

Now you have to remove the files in the src and spec folders.

Tests

In the spec folder you have to add a file for the tests. For this you create a file with the name helloworld_spec.js.

On the first line of the file you have to describe the Test:

  1. describe("Hello World",function(){

On the second line you have to explain, what the function should return:

  1. it("should return hello world",function(){

On the third line you have to compare the result with your expectation.

  1. expect(helloworld()).toEqual('Hello World')

On the last line the remaining clips are closed.

The whole file looks like this:

  1. describe("Hello World",function(){
  2. it("should return hello world",function(){
  3. expect(helloworld()).toEqual('Hello World')
  4. })
  5. });

Function

After creating the tests, we can create the function.

For this we create the file helloworld.js in the src folder.

This file extends the function with the name helloworld, which returns "Hello World".

The function looks like this:

  1. var helloworld = function(){
  2. return 'Hello World';
  3. };

Integrate

The files, which are necessary for the Test have to be declared in the file SpecRunner.html.

For this you have to find the both commented lines:

  1. <!-- include source files here... -->
  2. <!-- include spec files here... -->

The line have to be extended:

  1. <!-- include source files here... -->
  2. <script src="src/helloworld.js"></script>
  3. <!-- include spec files here... -->
  4. <script src="spec/helloworld.js"></script>

SpecRunner

To run the tests you have to open the SpecRunnder.html file in the browser.

There is a description for every test.