SlideShare a Scribd company logo
An Introduction to AngularJs Unit
Testing
Topics
•
What’re Karma & Jasmine?
•
Suites & Specs
•
beforeEach & afterEach (Setup & TearDown)
•
Expectations (Assertions)
•
Spies (Mocks & Stubs)
•
Exercise I (Hello World)
•
Exercise II (BMI Calculator Controller)
•
Q & A
Karma & Jasmine
Karma is a JavaScript command line tool that can
be used to spawn a web server which loads your
application's source code and executes your tests
(http://karma-runner.github.io/)
Jasmine provides functions to help with structuring
your tests and also making assertions. As your tests
grow, keeping them well structured and documented
is vital, and Jasmine helps achieve this.
(http://jasmine.github.io/)
Suites & Specs
Suites: describe()
A test suite begins with a call to the global Jasmine function
describe with two parameters: a string and a function. The
string is a name or title for a spec suite – usually what is being
tested. The function is a block of code that implements the
suite.
Specs: it()
specs are defined by calling the global Jasmine function it,
which, like describe takes a string and a function. The string
is the title of the spec and the function is the spec, or test. A
spec contains one or more expectations that test the state of
the code. An expectation in Jasmine is an assertion that is
either true or false. A spec with all true expectations is a
passing spec. A spec with one or more false expectations is a
failing spec.
Suites & Specs
(Con.)
PHP
class CalculatorTest extends
PHPUnit_Framework_TestCase{
public function
testCaculate2DigitsThenReturnTrue()
{
…
}
public function
testCaculate2CharsThenReturnFalse(){
…
}
}
Jasmine
describe("test calculator", function () {
it(“test calculate 2 digits then return true”,
function(){
…
});
it(“test calculate 2 chars then return true”,
function(){
…
});
});
beforeEach & afterEach (Setup &
Teardown)
PHP
class UnitTest extends PHPUnit_Framework_TestCase{
public function setUp(){
…
}
public function tearDown(){
…
}
}
Jasmine
describe("test hello world controller", function () {
beforeEach(function(){
….
});
afterEach(function () {
….
});
});
Expectations
(Assertions)
PHP VS Jasmine
assertContains(“world”, “hello world”) vs expect(“hello world”).toContain("world")
assertEquals(“success”, “success”) vs expect(“success”).toEqual(“success”)
assertNotEquals(“fail”, “success”) vs expect(“fail”).not.toEqual(“success”)
assertTrue(true) vs expect(true).toBeTruthy()
assertFalse(false) vs expect(false).toBeFalsy()
assertGreaterThan(1, 2) vs expect(2).toBeGreaterThan(1)
assertLessThan(2, 1) vs expect(1).toBeLessThan(2)
assertRegExp('/foo/', ‘yo ya foo’) vs expect(“yo ya foo”).toMatch(/foo/)
assertNull(null) vs expect(null).toBeNull()
assertNotNull(“not null”) vs expect(“not null”).not.toBeNull()
Spies (Mocks & Stubs)
PHP
public function setUp(){
$this->mock = $this
->getMockBuilder('HelloWorldClass')
->getMock();
$this->mock
->expects($this->once())
->method('printHelloWorld')
->with($this->anything())
->will($this->returnValue(“Hello World”));
}
Jasmine
beforeEach(function(){
spyOn(HelloWorldClass, “printHelloWorld”)
.and.returnValue(“Hello World);
});
it(“test printHelloWorld function”, function(){
expect(HelloWorldClass.printHelloWorld.calls.count
())
.toEqual(1);
expect(HelloWorldClass.printHelloWorld)
.
toHaveBeenCalledWith(jasmine.anything());
});
Exercise I
1.Clone https://github.com/inthra-onsap/angular-
unittesting-lab
2.Create spec file inside test/spec. (file_name:
hello_world.spec.js)
3.Put the code here inside:
"use strict"
var HelloWorldClass = {
printHelloWorld: function(){
return "Hello world";
}
};
describe("Hello world suite", function(){
it("test printHelloWorld method", function(){
expect(HelloWorldClass.printHelloWorld()).toEqual("Hello world”);
});
});
4. Open terminal and then execute “grunt test”
Exercise II
Body Mass Index(BMI) Calculation
BMI = weight/(height/100*height/100)
Specs:
1.To proof weight and height are GREATER THAN 0 (zero)
otherwise display “ERROR”.
2.To proof in case of BMI value is LOWER THAN or EQUAL
18.5 then display “You are too THIN”.
3.To proof in case of BMI value is GREATER THAN 18.5 and
LOWER THAN or EQUAL 25 then display “You are so
(HANDSOME or BEAUTIFUL)”.
4.To proof in case of BMI value is GREATER THAN 25 then
display “You are too FAT”.
One more thing…
CODE COVERAGE
Q & A

More Related Content

PDF
Angular testing
PPTX
Qunit Java script Un
PDF
Advanced Jasmine - Front-End JavaScript Unit Testing
PDF
Stop Making Excuses and Start Testing Your JavaScript
PDF
Quick tour to front end unit testing using jasmine
PDF
Unit tests in node.js
PDF
AngularJS Unit Testing w/Karma and Jasmine
PDF
Adventures In JavaScript Testing
Angular testing
Qunit Java script Un
Advanced Jasmine - Front-End JavaScript Unit Testing
Stop Making Excuses and Start Testing Your JavaScript
Quick tour to front end unit testing using jasmine
Unit tests in node.js
AngularJS Unit Testing w/Karma and Jasmine
Adventures In JavaScript Testing

What's hot (20)

PDF
Understanding JavaScript Testing
PDF
Intro to Unit Testing in AngularJS
PPTX
Understanding JavaScript Testing
PDF
JavaScript TDD with Jasmine and Karma
PDF
Describe's Full of It's
PPT
xUnit Style Database Testing
PPT
Krazykoder struts2 interceptors
PDF
JAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
PDF
Redux Thunk - Fu - Fighting with Async
PDF
Javascript tdd byandreapaciolla
PDF
MeetJS Summit 2016: React.js enlightenment
ODP
Angular JS Unit Testing - Overview
ODP
Unit Testing and Coverage for AngularJS
ODP
Unit testing with Easymock
PPTX
PPTX
Code generation for alternative languages
PDF
An introduction to Google test framework
PDF
Java custom annotations example
PDF
Tech In Asia PDC 2017 - Best practice unit testing in mobile apps
PDF
"Unit Testing for Mobile App" by Fandy Gotama (OLX Indonesia)
Understanding JavaScript Testing
Intro to Unit Testing in AngularJS
Understanding JavaScript Testing
JavaScript TDD with Jasmine and Karma
Describe's Full of It's
xUnit Style Database Testing
Krazykoder struts2 interceptors
JAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
Redux Thunk - Fu - Fighting with Async
Javascript tdd byandreapaciolla
MeetJS Summit 2016: React.js enlightenment
Angular JS Unit Testing - Overview
Unit Testing and Coverage for AngularJS
Unit testing with Easymock
Code generation for alternative languages
An introduction to Google test framework
Java custom annotations example
Tech In Asia PDC 2017 - Best practice unit testing in mobile apps
"Unit Testing for Mobile App" by Fandy Gotama (OLX Indonesia)
Ad

Viewers also liked (15)

PDF
Angular js, Yeomon & Grunt
PDF
Grunt js and WordPress
PDF
Mastering Grunt
PDF
GruntJS + Wordpress
ODP
Wrangling the WordPress Template Hierarchy Like a Boss
PPTX
WordPress Theme Development Workflow with Node.js, Ruby, Sass, Bower and Grunt
PPTX
Metadata and me
PDF
A Quick and Dirty D3.js Tutorial
PDF
Come migliorare le performance di WordPress con il Visual Composer
PDF
Javascript testing: tools of the trade
PDF
Using Composer to create manageable WordPress websites
PDF
WordPress Database: What's behind those 12 tables
PDF
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
PDF
WordPress Template Hierarchy
PDF
Getting Started With Grunt for WordPress Development
Angular js, Yeomon & Grunt
Grunt js and WordPress
Mastering Grunt
GruntJS + Wordpress
Wrangling the WordPress Template Hierarchy Like a Boss
WordPress Theme Development Workflow with Node.js, Ruby, Sass, Bower and Grunt
Metadata and me
A Quick and Dirty D3.js Tutorial
Come migliorare le performance di WordPress con il Visual Composer
Javascript testing: tools of the trade
Using Composer to create manageable WordPress websites
WordPress Database: What's behind those 12 tables
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
WordPress Template Hierarchy
Getting Started With Grunt for WordPress Development
Ad

Similar to An Introduction to AngularJs Unittesting (20)

PDF
Quick Tour to Front-End Unit Testing Using Jasmine
PDF
Quick tour to front end unit testing using jasmine
PDF
Front end unit testing using jasmine
PPTX
Unit testing JavaScript: Jasmine & karma intro
PPTX
Unit testing in JavaScript with Jasmine and Karma
PPTX
Unit testing of java script and angularjs application using Karma Jasmine Fra...
PPTX
PPTX
Angular Unit Testing
PPTX
Unit testing using jasmine in Javascript
PDF
Angularjs - Unit testing introduction
PPTX
Testing angular js
PDF
What the HTML? - The Holy Grail
PDF
AngularJS Unit Test
PPTX
Javascript Testing with Jasmine 101
PDF
3 WAYS TO TEST YOUR COLDFUSION API
PDF
3 WAYS TO TEST YOUR COLDFUSION API -
PPTX
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
PDF
JavaCro'14 - Unit testing in AngularJS – Slaven Tomac
PPTX
Jasmine framework
PPTX
Angular Unit testing.pptx
Quick Tour to Front-End Unit Testing Using Jasmine
Quick tour to front end unit testing using jasmine
Front end unit testing using jasmine
Unit testing JavaScript: Jasmine & karma intro
Unit testing in JavaScript with Jasmine and Karma
Unit testing of java script and angularjs application using Karma Jasmine Fra...
Angular Unit Testing
Unit testing using jasmine in Javascript
Angularjs - Unit testing introduction
Testing angular js
What the HTML? - The Holy Grail
AngularJS Unit Test
Javascript Testing with Jasmine 101
3 WAYS TO TEST YOUR COLDFUSION API
3 WAYS TO TEST YOUR COLDFUSION API -
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
JavaCro'14 - Unit testing in AngularJS – Slaven Tomac
Jasmine framework
Angular Unit testing.pptx

An Introduction to AngularJs Unittesting

  • 1. An Introduction to AngularJs Unit Testing
  • 2. Topics • What’re Karma & Jasmine? • Suites & Specs • beforeEach & afterEach (Setup & TearDown) • Expectations (Assertions) • Spies (Mocks & Stubs) • Exercise I (Hello World) • Exercise II (BMI Calculator Controller) • Q & A
  • 3. Karma & Jasmine Karma is a JavaScript command line tool that can be used to spawn a web server which loads your application's source code and executes your tests (http://karma-runner.github.io/) Jasmine provides functions to help with structuring your tests and also making assertions. As your tests grow, keeping them well structured and documented is vital, and Jasmine helps achieve this. (http://jasmine.github.io/)
  • 4. Suites & Specs Suites: describe() A test suite begins with a call to the global Jasmine function describe with two parameters: a string and a function. The string is a name or title for a spec suite – usually what is being tested. The function is a block of code that implements the suite. Specs: it() specs are defined by calling the global Jasmine function it, which, like describe takes a string and a function. The string is the title of the spec and the function is the spec, or test. A spec contains one or more expectations that test the state of the code. An expectation in Jasmine is an assertion that is either true or false. A spec with all true expectations is a passing spec. A spec with one or more false expectations is a failing spec.
  • 5. Suites & Specs (Con.) PHP class CalculatorTest extends PHPUnit_Framework_TestCase{ public function testCaculate2DigitsThenReturnTrue() { … } public function testCaculate2CharsThenReturnFalse(){ … } } Jasmine describe("test calculator", function () { it(“test calculate 2 digits then return true”, function(){ … }); it(“test calculate 2 chars then return true”, function(){ … }); });
  • 6. beforeEach & afterEach (Setup & Teardown) PHP class UnitTest extends PHPUnit_Framework_TestCase{ public function setUp(){ … } public function tearDown(){ … } } Jasmine describe("test hello world controller", function () { beforeEach(function(){ …. }); afterEach(function () { …. }); });
  • 7. Expectations (Assertions) PHP VS Jasmine assertContains(“world”, “hello world”) vs expect(“hello world”).toContain("world") assertEquals(“success”, “success”) vs expect(“success”).toEqual(“success”) assertNotEquals(“fail”, “success”) vs expect(“fail”).not.toEqual(“success”) assertTrue(true) vs expect(true).toBeTruthy() assertFalse(false) vs expect(false).toBeFalsy() assertGreaterThan(1, 2) vs expect(2).toBeGreaterThan(1) assertLessThan(2, 1) vs expect(1).toBeLessThan(2) assertRegExp('/foo/', ‘yo ya foo’) vs expect(“yo ya foo”).toMatch(/foo/) assertNull(null) vs expect(null).toBeNull() assertNotNull(“not null”) vs expect(“not null”).not.toBeNull()
  • 8. Spies (Mocks & Stubs) PHP public function setUp(){ $this->mock = $this ->getMockBuilder('HelloWorldClass') ->getMock(); $this->mock ->expects($this->once()) ->method('printHelloWorld') ->with($this->anything()) ->will($this->returnValue(“Hello World”)); } Jasmine beforeEach(function(){ spyOn(HelloWorldClass, “printHelloWorld”) .and.returnValue(“Hello World); }); it(“test printHelloWorld function”, function(){ expect(HelloWorldClass.printHelloWorld.calls.count ()) .toEqual(1); expect(HelloWorldClass.printHelloWorld) . toHaveBeenCalledWith(jasmine.anything()); });
  • 9. Exercise I 1.Clone https://github.com/inthra-onsap/angular- unittesting-lab 2.Create spec file inside test/spec. (file_name: hello_world.spec.js) 3.Put the code here inside: "use strict" var HelloWorldClass = { printHelloWorld: function(){ return "Hello world"; } }; describe("Hello world suite", function(){ it("test printHelloWorld method", function(){ expect(HelloWorldClass.printHelloWorld()).toEqual("Hello world”); }); }); 4. Open terminal and then execute “grunt test”
  • 10. Exercise II Body Mass Index(BMI) Calculation BMI = weight/(height/100*height/100) Specs: 1.To proof weight and height are GREATER THAN 0 (zero) otherwise display “ERROR”. 2.To proof in case of BMI value is LOWER THAN or EQUAL 18.5 then display “You are too THIN”. 3.To proof in case of BMI value is GREATER THAN 18.5 and LOWER THAN or EQUAL 25 then display “You are so (HANDSOME or BEAUTIFUL)”. 4.To proof in case of BMI value is GREATER THAN 25 then display “You are too FAT”.
  • 12. Q & A