Behavior Driven Development (JASMINE)
BDD and TDD
BDD and TDD … stand for Behavior-Driven
Development and Test-Driven Development.
TDD in its simplest form is just this:
Ø
Write your tests
Ø
Watch them fail
Ø
Make them pass
Ø
Refactor
Ø
Repeat
That’s
pretty easy to understand, eh?
Introduction
Behavior Driven Development (BDD) is a software
development process that originally emerged from Test Driven Development (TDD).
The BDD methodology is an extension of test
driven development (TDD).
Both advocate that tests should be written first,
which for BDD this means acceptance tests, followed by unit tests driven by the
acceptance tests.
BDD focus is on capturing the required behavior in
User Stories, and from these acceptance tests are written.
Idea behind BDD
The idea with acceptance testing is to write tests (or
behavioral specifications) that describe the behavior of your software in a
language which is not code but is more precise than standard English.
Doing this allows people who are not software
engineers, but have knowledge of the requirements, such as Product Management
or Marketing, to write the scenarios that make up our ATs. This is a powerful
thing when it comes to capturing the required behavior.
Different Frameworks Available for BDD
#1 Mocha
#2 Jasmine
#3 Cucumber
What is KARMA?
Karma is neither a testing framework nor an assertion
library but is a tool that allows you to run your JavaScript test cases.
Karma supports many JavaScript testing frameworks and
Jasmine is one of them.
Karma also supports running your test cases inside
various browsers and through command line.
You can configure Karma to run locally on your
development environment as well as CI (Continuous Integration) Server.
Why Jasmine
Jasmine is a behavior-driven development framework for
testing JavaScript code that plays very well with Karma. Similar to Karma, it’s
also the recommended testing framework within the AngularJS documentation.
Jasmine is also dependency free and doesn’t require a DOM.
What a Jasmine Suite Looks Like
At least one describe block (they can be
nested)
At least one it block which contains a spec
At least one expectation, which consists of:
expect which is passed an expression (called the
"actual" value)
A matcher with the expected value
(toBe and toBeGreaterThan)
Suites: describe Your
Tests
Suite’s are like class in object oriented languages
like Java, C# which can contain one or more test case methods.
In Jasmine you can define a suite by calling global
Jasmine global function called describe () which takes two arguments.
The first argument is the name of the suite and the
second argument is the JavaScript function containing initialization functions
and specs.
Specs
Spec in Jasmine is a test case which can be defined by
calling global Jasmine function called it () which again takes two arguments
like suite.
The first
argument is the name of the spec and the second argument is the JavaScript
function containing one or more expectations.
If one run’s any of the spec temporarily then you can
declare it as it(), these spec are called pending specs.
Expectations
Expectation in Jasmine is like assertions. Expectation
is built with Jasmine function called expect ().
Expect function takes a single argument which
represents the actual value and compares it to the expected value with the help
of a matcher function.
Jasmine’s matcher functions do Boolean comparison
between the actual and the expected values.
Jasmine framework is loaded with plenty of matchers
but if there is a need you can also define you own custom matcher.
Setup and Teardown
You can implement initialization and cleanup logic
inside the Jasmine’s following global functions.
The beforeEach() is called once before every spec in
the describe is executed
The afterEach() is called once after every spec in the
describe is executed
The beforeAll() is called only once before any of the
specs in the describe is executed
The afterAll() is called after all the specs in the
describe are executed
Beginner's example: hello
world
What you want to test
function helloWorld() {
return "Hello
world!";
}
JASMINE Spec
describe("Hello
world", function() { -- suite
it("says hello", function()
{ -- spec expect(helloWorld()).toEqual("Hello
world!"); -- matcher
});
});
karma-jasmine-feature
I can see no difference between BDD and TDD using
JASMINE.
So if you want to use gherkin langugae BDD, then
there is a Karma plugin that use gherkin
feature files to describe tests with Jasmine.
karma-jasmine-feature