An overview about testing and Jasmine



by




Jan 23rd, 2014

Agenda

  • Prerequisites
  • Test First
  • TDD
  • Jasmine
    • describe
    • it
    • expect
    • matcher
  • jasmine-node
  • Plunker
  • A challenge
______________________________________________
An  overview about testing and Jasmine

Prerequisites



JavaScript beginner
or all level



if you already know how to do a sample line using JavaScript,
feel free to join...
______________________________________________
An  overview about testing and Jasmine

Test First


Each time you start writing a new class, 
a new module,
or just a new piece of logic,
ask yourself:  How can I test this?
______________________________________________
An  overview about testing and Jasmine

How can I test this?


The answer to this question will help you to code much more efficiently, create better APIs, and put everything into nicely separated blocks.

You can't write tests for spaghetti code.
______________________________________________
An  overview about testing and Jasmine

TDD


in my own words
it's a way for improving the code design by testing.


a formal definition
write tests before code following a small iteration: 
red, green, refactor cycle.


______________________________________________
An  overview about testing and Jasmine

Jasmine 1.3.1

A framework for testing JavaScript code


describe("A suite", function() {
  it("contains spec with an expectation", function() {
    expect(true).toBe(true);
  });
}); 


______________________________________________
An  overview about testing and Jasmine

Describe


A test suite that contains one or more specs. 

Also it might have other test suites.
______________________________________________
An  overview about testing and Jasmine

It


A spec that contains one or more expectations.
______________________________________________
An  overview about testing and Jasmine

Expect


An expectation between the actual and the expected value.

It's basically a boolean comparison between two values. Each expectation will be responsible for reporting true or false.
Then we'll be able to see a passed or failed spec.
______________________________________________
An  overview about testing and Jasmine

Matcher


It's located between the actual and the expected value.

Examples:

  • toEqual
  • toBe
  • toMatch
  • toBeDefined
  • toBeUndefined
  • toBeNull
  • toBeTruthy
  • toBeFalsy
  • toContain
  • toBeLessThan
  • toBeGreaterThan
  • toThrow


Any matcher can evaluate to a negative assertion by chaining the call to expect with a not before calling the matcher.
______________________________________________
An  overview about testing and Jasmine

jasmine-node


A NodeJS module that makes the wonderful Jasmine framework available in NodeJS.
______________________________________________
An  overview about testing and Jasmine

Install jasmine-node


$ npm install jasmine-node -g
______________________________________________
An  overview about testing and Jasmine

An example


It's based from  the jasmine documentation.
Keep the documentation in mind.

Any question?
Please go there first.
______________________________________________
An  overview about testing and Jasmine

my-code.spec.js


describe("A suite", function() {
  it("contains spec with an expectation", function() {
    expect(true).toBe(true);
  });
}); 

______________________________________________
An  overview about testing and Jasmine

Run jasmine-node


jasmine-node my-code.spec.js --color --autotest --watch ./
______________________________________________
An  overview about testing and Jasmine

my-code.js

jasmine-node should be running...

exports.MyObject = {
  
}; 

______________________________________________
An  overview about testing and Jasmine

Enhancing our spec

jasmine-node should be running...


describe("My new object", function() {

  var MyObject = require('./my-code.js').MyObject;

  it("should have a property named 'foo'", function() {
    expect(MyObject.foo).toBeDefined();
  });

}); 

test should FAIL
______________________________________________
An  overview about testing and Jasmine

Enhancing our code

jasmine-node should be running...

exports.MyObject = {
  foo: {}
}; 




test should PASS
______________________________________________
An  overview about testing and Jasmine

Plunker


An online community for
creating,
collaborating on
and sharing
your web development ideas.


It's amazing!!!
Love it!!!!

______________________________________________
An  overview about testing and Jasmine

Our Jasmine Tests on Plunker


Go to:


There is no excuse.
You can develop your idea online and on real time, keeping in mind doing the test first.


Isn't it amazing? I know it is!!!
______________________________________________
An  overview about testing and Jasmine

Conclusion


Keep in mind "Test First".

Using Jasmine is a way to do that.
Keep it in mind.
______________________________________________
An  overview about testing and Jasmine

Learn more

go futher with the reference links





______________________________________________
An  overview about testing and Jasmine

A challenge


"My Calculator"

  "sum 2 + 2 SHOULD be 4"
  "sum 2 + 2 + 3 + 1 SHOULD be 8"

  "subtraction between 2 and 2 SHOULD be 0"
  "subtraction between 3 and 4 SHOULD be -1"

  "multiplication between 3 and 2 SHOULD be 6"
  "multiplication between 3, 2 and 4 SHOULD be 24"

  "division between 2 and 2 SHOULD be 1"
  "division between 6 and 2 SHOULD be 3" 

That's it
______________________________________________
An  overview about testing and Jasmine

Thanks


Thanks for your time.

Hope it might be helpful for you all.






Thanks again!!!
______________________________________________
An  overview about testing and Jasmine

An overview about jasmine testing

By Igor Ribeiro Lima

An overview about jasmine testing

An overview how we can improve our code design by testing with Jasmine framework

  • 2,587