Jump to content

User:Lkommi/sandbox

From Wikipedia, the free encyclopedia

Jasmine is an automated testing framework that is behaviour driven, developed by Pivotal labs and commonly used for JavaScript code. Any javascript code can be tested against specifications given by the user.[1]The software is based on unit testing. This framework is popular as it does not need any Documentation Object Model (DOM) and it does not have any dependencies.[2] Due to the rise of mobile applications which need JavaScript, Jasmine has become one of most used testing softwares. The software has “MIT Licence” and works across Operating Systems. Jasmine can be used anywhere JavaScript is used; from websites to Node.js. Jasmine can run on frameworks like Sinatra and Ruby on Rails but has a standalone version of it as well. Using the Jasmine Ruby gem, testing can be done on Ruby code as well.

Version History

[edit]

The first release of the Jasmine framework was in September 14, 2010.[3] The version Jasmine 2.3 was the first stable release.

Version Release date
2.5.0 09/01/2016
2.4.0 12/03/2015
2.3.0 04/28/2015
2.0.0 12/16/2013
1.3.1 12/03/2012

Jasmine Ruby Gem

[edit]

Jasmine Ruby or Jasmine Ruby gem is a package that is used for Ruby programming based web development projects. These projects primarily use Sinatra or Ruby on Rails.[4] The package file is used with the Jasmine testing framework and is also compatible with JavaScript codes. At the same time, the Jasmine Ruby Gem is not dependant on any framework(s) but can be used with frameworks like Sinatra or the Ruby on Rails framework.

Setup

[edit]

The installation, initialisation and usage of jasmine in the Rails framework allows us to test javascript without making changes to the spec HTML file. Jasmine can also be installed as a standalone package. [5]

Installation

[edit]

Jasmine needs the gem files and bundle to be installed before any testing. The "bundle install" command installs all the necessary dependencies to the Gem file.

$ gem "jasmine"
$ bundle install 
$ rails generate jasmine:install

Initialisation

[edit]

If the project is using Rails, jasmine has generators that can be used to set everything up. the "rails generate" command is very useful in reducing the code needed to write. Time is saved on the part of the code that is mandatory for the working of the application. This uses the template to simplify working.

$ rails generate jasmine:install
$ rails generate jasmine:examples

Usage

[edit]

Once jasmine is installed, two commands will be available in rake. rake jasmine command is used to start the jasmine server. rake jasmine:ci is used for continuous integration environments. This is needed for projects where multiple users are utilising a shared repository to make any changes. The changes will only go through once all the test cases have been passed.

$ rake jasmine
$ rake jasmine:ci

Example

[edit]

The JavaScript example taken, prints "This is a test!". Jasmine is used to test whether the code will give the expected output.

function test() {
  return "This is a test!";
}

Given below is the test suite for the above JavaScript code. The function describe( ) takes two arguments, a string which is the title for the test suite and a function which implements it.[6] The function it( ) defines a spec which describes the expected output of a piece of code, by taking two arguments, a string and a function. Matchers are used in Jasmine to compare the output of a program with an argument. In this example, toEqual matcher is used to test whether the output of the program is "This is a test!". Several other matchers are inbuilt in Jasmine and matchers can also be user-defined.

describe("Jasmine Test", function() {
   it("prints that this is a test", function() {
      expect(test()).toEqual("This is a test!"); 
    });
 });

See also

[edit]

References

[edit]
  1. ^ Hahn, Evan (2013). JavaScript Testing with Jasmine. O'Reilly Media, Inc.
  2. ^ "Jasmine Documentation on GitHub".
  3. ^ "Pivotal labs".
  4. ^ "Ruby gems documentation".
  5. ^ "Installation of Jasmine gem".
  6. ^ "Testing JavaScript code using Jasmine".