Dev in Bahia 1º First
Technical Meeting
What is Dev In Bahia ?
• Borned in 2012 through the #horaextra.
• Has the vision to transform our local IT Market in a better
place to work and to develop ourselves as IT Professionals.
• What we do ?
• Promote Events, Discussions, User groups and so on.
Technical Meetings
• We are trying to promete one since last year.
• Prevented by:
• Place
• People
• Schedule
• We want to promote a technical meeting once or twice a
month.
• Tech Talk + Coding Dojo
• Before each meeting, people will send talk suggestions and we
are going to vote to choose.
Who is Paulo Ortins ?
• Developer at Inteligência Digital
• Masters Student at UFBA ( Software Engineering)
• Blogger at www.pauloortins.com
• Newsletter Curater at dotnetpills.apphb.com
• Joined in the community in 2011
• Polyglot Programmer
• Founded Dev In Bahia and #horaextra
Twitter: @pauloortins
Github: pauloortins
1º Technical Meeting
• How create tests using Javascript ?
A test overview
• Monkey Tests
• Unit Tests
• End-to-End Tests
Monkey Tests
But Requirements Change
You do it again
Monkey Test [2]
But Requirements Change[2]
Monkey Test Division
Problems with Monkey Tests
• Low Reliability, people aren’t machines, they work has
variance.
• Expensive, people have to test the software everytime
something changes.
Super Kent Beck
Automated Tests
• Tests should be automated.
• Functionality are done if, and only if, there are automated
tests covering them.
Unit Tests
• Tests only a piece of code.
• Provide instantly feedback about our software.
• Help to improve code design.
Example
function isBetweenFiveAndTen(number) {
var isGreaterThanFive = number > 5;
var isLesserThanTen = number < 10;
return isGreaterThanFive && isLesserThanTen;
}
Input Output
5 False
6 True
7 True
10 False
End-to-end Tests
• Tests simulate a monkey test, covering browser interaction,
database access, business rules.
• Slower than unit tests.
• Let me show a example using Selenium WebDriver
Javascript
• Javascript is rising.
• Web more interactive and responsive.
• Applications like Facebook and Gmail.
• Web Apps ( PhoneGap, Ext.js, jquery mobile)
Unit tests in Javascript
• There are several options to create unit tests in Javascript:
• Qunit
• Mocha
• Jasmine
Jasmine
• Created due the dissatisfaction existing framworks by
PivotalLabs.
• Small library
• Easy to use
Suites
describe("A suite", function() {
it("contains spec with an expectation", function() {
expect(true).toBe(true);
});
});
• Describe, name a test suite ou a set test.
• It, describe the test name.
Expectations
describe("The 'toBe' matcher compares with ===", function() {
it("and has a positive case ", function() {
expect(true).toBe(true);
});
it("and can have a negative case", function() {
expect(false).not.toBe(true);
});
});
• Comparisons made through matchers, who are predefined
functions who receives a value (actual) and compares with the
expected value.
• A lot of matchers are included.
Matchers
describe("The 'toEqual' matcher", function() {
it("works for simple literals and variables", function() {
var a = 12;
expect(a).toEqual(12);
});
it("should work for objects", function() {
var foo = {
a: 12,
b: 34
};
var bar = {
a: 12,
b: 34
};
expect(foo).toEqual(bar);
});
});
Matchers
it("The 'toMatch' matcher is for regular expressions", function() {
var message = 'foo bar baz';
expect(message).toMatch(/bar/);
expect(message).toMatch('bar');
expect(message).not.toMatch(/quux/);
});
Matchers
it("The 'toBeDefined' matcher compares against `undefined`",
function() {
var a = {
foo: 'foo'
};
expect(a.foo).toBeDefined();
expect(a.bar).not.toBeDefined();
});
Matchers
it("The 'toBeNull' matcher compares against null", function() {
var a = null;
var foo = 'foo';
expect(null).toBeNull();
expect(a).toBeNull();
expect(foo).not.toBeNull();
});
Matchers
it("The 'toBeTruthy' matcher is for boolean casting testing", function()
{
var a, foo = 'foo';
expect(foo).toBeTruthy();
expect(a).not.toBeTruthy();
});
it("The 'toBeFalsy' matcher is for boolean casting testing", function() {
var a, foo = 'foo';
expect(a).toBeFalsy();
expect(foo).not.toBeFalsy();
});
Matchers
it("The 'toContain' matcher is for finding an item in an Array",
function() {
var a = ['foo', 'bar', 'baz'];
expect(a).toContain('bar');
expect(a).not.toContain('quux');
});
Matchers
it("The 'toBeLessThan' matcher is for mathematical comparisons", function() {
var pi = 3.1415926, e = 2.78;
expect(e).toBeLessThan(pi);
expect(pi).not.toBeLessThan(e);
});
it("The 'toBeGreaterThan' is for mathematical comparisons", function() {
var pi = 3.1415926, e = 2.78;
expect(pi).toBeGreaterThan(e);
expect(e).not.toBeGreaterThan(pi);
});
it("The 'toBeCloseTo' matcher is for precision math comparison", function() {
var pi = 3.1415926, e = 2.78;
expect(pi).not.toBeCloseTo(e, 2);
expect(pi).toBeCloseTo(e, 0);
});
Matchers
it("The 'toThrow' matcher is for testing if a function throws an
exception", function() {
var foo = function() {
return 1 + 2;
};
var bar = function() {
return a + 1;
};
expect(foo).not.toThrow();
expect(bar).toThrow();
});
Custom Matchers
beforeEach(function() {
this.addMatchers({
isEven: function(number) {
return number % 2 === 0;
}
});
});
Setup/Teardown
describe("A spec (with setup and tear-down)", function() {
var foo;
beforeEach(function() {
foo = 0;
foo += 1;
});
afterEach(function() {
foo = 0;
});
it("is just a function, so it can contain any code", function() {
expect(foo).toEqual(1);
});
it("can have more than one expectation", function() {
expect(foo).toEqual(1);
expect(true).toEqual(true);
});
});
Let’s play with Jasmine
• Site
• Tutorial
• Standalone Version
Testacular/Karma
• Created by Google to test Angular.js
• Runs on top of Node.js
• Watch our JS files to detect changes and rerun the tests
Thank you!

Tests in Javascript using Jasmine and Testacular

  • 1.
    Dev in Bahia1º First Technical Meeting
  • 2.
    What is DevIn Bahia ? • Borned in 2012 through the #horaextra. • Has the vision to transform our local IT Market in a better place to work and to develop ourselves as IT Professionals. • What we do ? • Promote Events, Discussions, User groups and so on.
  • 3.
    Technical Meetings • Weare trying to promete one since last year. • Prevented by: • Place • People • Schedule • We want to promote a technical meeting once or twice a month. • Tech Talk + Coding Dojo • Before each meeting, people will send talk suggestions and we are going to vote to choose.
  • 4.
    Who is PauloOrtins ? • Developer at Inteligência Digital • Masters Student at UFBA ( Software Engineering) • Blogger at www.pauloortins.com • Newsletter Curater at dotnetpills.apphb.com • Joined in the community in 2011 • Polyglot Programmer • Founded Dev In Bahia and #horaextra Twitter: @pauloortins Github: pauloortins
  • 5.
    1º Technical Meeting •How create tests using Javascript ?
  • 6.
    A test overview •Monkey Tests • Unit Tests • End-to-End Tests
  • 7.
  • 8.
  • 9.
    You do itagain
  • 10.
  • 11.
  • 12.
  • 13.
    Problems with MonkeyTests • Low Reliability, people aren’t machines, they work has variance. • Expensive, people have to test the software everytime something changes.
  • 14.
  • 15.
    Automated Tests • Testsshould be automated. • Functionality are done if, and only if, there are automated tests covering them.
  • 16.
    Unit Tests • Testsonly a piece of code. • Provide instantly feedback about our software. • Help to improve code design.
  • 17.
    Example function isBetweenFiveAndTen(number) { varisGreaterThanFive = number > 5; var isLesserThanTen = number < 10; return isGreaterThanFive && isLesserThanTen; } Input Output 5 False 6 True 7 True 10 False
  • 18.
    End-to-end Tests • Testssimulate a monkey test, covering browser interaction, database access, business rules. • Slower than unit tests. • Let me show a example using Selenium WebDriver
  • 19.
    Javascript • Javascript isrising. • Web more interactive and responsive. • Applications like Facebook and Gmail. • Web Apps ( PhoneGap, Ext.js, jquery mobile)
  • 20.
    Unit tests inJavascript • There are several options to create unit tests in Javascript: • Qunit • Mocha • Jasmine
  • 21.
    Jasmine • Created duethe dissatisfaction existing framworks by PivotalLabs. • Small library • Easy to use
  • 22.
    Suites describe("A suite", function(){ it("contains spec with an expectation", function() { expect(true).toBe(true); }); }); • Describe, name a test suite ou a set test. • It, describe the test name.
  • 23.
    Expectations describe("The 'toBe' matchercompares with ===", function() { it("and has a positive case ", function() { expect(true).toBe(true); }); it("and can have a negative case", function() { expect(false).not.toBe(true); }); }); • Comparisons made through matchers, who are predefined functions who receives a value (actual) and compares with the expected value. • A lot of matchers are included.
  • 24.
    Matchers describe("The 'toEqual' matcher",function() { it("works for simple literals and variables", function() { var a = 12; expect(a).toEqual(12); }); it("should work for objects", function() { var foo = { a: 12, b: 34 }; var bar = { a: 12, b: 34 }; expect(foo).toEqual(bar); }); });
  • 25.
    Matchers it("The 'toMatch' matcheris for regular expressions", function() { var message = 'foo bar baz'; expect(message).toMatch(/bar/); expect(message).toMatch('bar'); expect(message).not.toMatch(/quux/); });
  • 26.
    Matchers it("The 'toBeDefined' matchercompares against `undefined`", function() { var a = { foo: 'foo' }; expect(a.foo).toBeDefined(); expect(a.bar).not.toBeDefined(); });
  • 27.
    Matchers it("The 'toBeNull' matchercompares against null", function() { var a = null; var foo = 'foo'; expect(null).toBeNull(); expect(a).toBeNull(); expect(foo).not.toBeNull(); });
  • 28.
    Matchers it("The 'toBeTruthy' matcheris for boolean casting testing", function() { var a, foo = 'foo'; expect(foo).toBeTruthy(); expect(a).not.toBeTruthy(); }); it("The 'toBeFalsy' matcher is for boolean casting testing", function() { var a, foo = 'foo'; expect(a).toBeFalsy(); expect(foo).not.toBeFalsy(); });
  • 29.
    Matchers it("The 'toContain' matcheris for finding an item in an Array", function() { var a = ['foo', 'bar', 'baz']; expect(a).toContain('bar'); expect(a).not.toContain('quux'); });
  • 30.
    Matchers it("The 'toBeLessThan' matcheris for mathematical comparisons", function() { var pi = 3.1415926, e = 2.78; expect(e).toBeLessThan(pi); expect(pi).not.toBeLessThan(e); }); it("The 'toBeGreaterThan' is for mathematical comparisons", function() { var pi = 3.1415926, e = 2.78; expect(pi).toBeGreaterThan(e); expect(e).not.toBeGreaterThan(pi); }); it("The 'toBeCloseTo' matcher is for precision math comparison", function() { var pi = 3.1415926, e = 2.78; expect(pi).not.toBeCloseTo(e, 2); expect(pi).toBeCloseTo(e, 0); });
  • 31.
    Matchers it("The 'toThrow' matcheris for testing if a function throws an exception", function() { var foo = function() { return 1 + 2; }; var bar = function() { return a + 1; }; expect(foo).not.toThrow(); expect(bar).toThrow(); });
  • 32.
    Custom Matchers beforeEach(function() { this.addMatchers({ isEven:function(number) { return number % 2 === 0; } }); });
  • 33.
    Setup/Teardown describe("A spec (withsetup and tear-down)", function() { var foo; beforeEach(function() { foo = 0; foo += 1; }); afterEach(function() { foo = 0; }); it("is just a function, so it can contain any code", function() { expect(foo).toEqual(1); }); it("can have more than one expectation", function() { expect(foo).toEqual(1); expect(true).toEqual(true); }); });
  • 34.
    Let’s play withJasmine • Site • Tutorial • Standalone Version
  • 35.
    Testacular/Karma • Created byGoogle to test Angular.js • Runs on top of Node.js • Watch our JS files to detect changes and rerun the tests
  • 36.