up & running with jasmine

18
Up & Running with Jasmine unit testing JavaScript Md. Kamarul Kawnayeen

Upload: kamarul-kawnayeen

Post on 06-Aug-2015

76 views

Category:

Technology


3 download

TRANSCRIPT

Up & Running with Jasmineunit testing JavaScript

Md. Kamarul Kawnayeen

Preface

I’m assuming that you write your test code first, before writing your production code. And when we are talking about a language like JavaScript, we have no way to ignore unit testing. It has no static typing, no way to make a specific variable or method private, so many different vendor (like chrome, safari, firefox etc). If something went wrong, we just don’t know what went wrong.

Here I’ll show how to up & running with Jasmine for testing your JavaScript code.

go here & download WebStorm

After installing, run WebStorm & select “Create New Project”

Select “Empty Project” and then select the location & hit “Create”

here we are with a blank project

go here & download the latest standalone version

Unzip it, then copy the lib directory & SpecRunner.html

Now go to your WebStorm project, right click on the project name & Paste

select ok & those files and folder will be in our project

open Specrunner.html & remove the source & spec files

create a new directory scripts and within that directory create two new js file, math.js & math-spec.js

math.js is our source file & math-spec.js is our spec file

As we are good developers :) , so we should write our test code first. Open math-spec.js & write this simple test code.

describe(string, callback_function) and it(string, callback_function) are two functions of Jasmine framework. check details about them here.

Open the SpecRunner.html & add the new source and spec file

Now right click on SpecRunner.html & select Run “SpecRunner.html”

Well, we ran our unit test & it fails. Don’t worry, we didn’t write our production code yet!!!

Open math.js & implement the sum function

Run SpecRunner.html again & yeah!! we pass this time.