The document discusses testing JavaScript code using the Jasmine testing framework. It provides an overview of Jasmine, including how to write Jasmine tests using suites, specs, matchers and spies. It also covers how to set up before and after each/all blocks, create custom matchers, and get the latest version of Jasmine from GitHub. Example code for using Jasmine is available at a provided URL.
13. Spies
Spy on a method
spyOn(obj, ‘method’)
Verify its been called
Expect(obj.method).toHaveBeenCalled()
Verify its been called
Expect(obj.method).toHaveBeenCalled
With(‘x’,’y’)
14. Spies continued……
Fix the return value of the spy
spyOn(obj, ‘method’).andReturn(‘Return me’)
How many times has it been called?
Obj.method.callcount
What were the arguments to the last call
Obj.mthod.mostrecentCall.args
Reset calls made to the spy so far
Object.method.reset()
15. Spies continued……
Obj.method =Jasmine.createSpy(“spy Name”);
jasmine.createSpy("spy Name
").andReturn(“Return Value");
jasmine.createSpy(" spy Name
").andCallFake(function() {
do something;
return “Return Value";
});
16. Get Jasmine
You can get the latest version of Jasmine
from:-
https://github.com/jasmine/jasmine/rel
eases
18. The sweet smell of Jasmine
when testing JavaScript
Emma Armstrong
@EmmaATester
www.taooftesting.co.uk
Emma.armstrong@towerswatson.com
Image: Jasmine by Ken Bosma
Editor's Notes
Why test?
With the move to agile, there has been a large emphasis placed on testing early
As well as business demands to provide value to customers often. To meet both these needs it has been important to look at ways to automate some of the tests as they can be run quickly and frequently, providing feedback in a methodology where requirements and products are changing frequently.
Why test websites?
If your website happens to be the first port of call for someone, an issue in it can affect their confidence in your company or brand.
Competitive edge
Why automate?
You can not automate everything, could many automation tests find that what you thought should look like this (insert screen shot) actually looked like this (insert screen shot). The answer is that they could but is that the best use of automation and so no your tests would be unlikely to find this sort of thing and so no automation does not replace the need for some manual testing.
When choosing what to automate you should consider the value a test adds and the cost it would take to implement.
People may have heard that the first time you run an automated test it is testing and then each subsequent run is merely checking. This is true but that checking can provide good information about the stability and functionality of latest version of the product after changes were made. Make sure to be ‘mindful’ about what you want to test.
This means you need to think about what you should automate and how to do that automation.
Automation frameworks need to be robust and provide sufficient feedback that the system is still behaving as it did previously.
Why test JavaScript?
Be Aware of using JavaScript
Disabled JavaScript
Browsers/Devices not supporting JavaScript
Accessibility
Navigation
Hidden Content
User control
Confusion/Disorientation
Consider being device independent – not requiring only a mouse or a keyboard – making that information available to assistive technologies.
The BBC reported that 73% of websites relied on JavaScript for important functionality which meant that they fell short of the minimum standards set out by the Web Content Accessibility Guidelines (WCAG) due to the JavaScript not working with screen readers.
“A further 73% failed to make the grade because of their reliance on JavaScript for some of the website's functionality. JavaScript does not work with some screen readers used by those with impaired vision.” From http://news.bbc.co.uk/1/hi/technology/6210068.stm
What is Jasmine?
The Jasmine website is hosted by GitHub http://jasmine.github.io/ The latest version is 2.2 or you can be on the bleeding edge version.
Jasmine itself is in github https://github.com/jasmine/jasmine
Jasmine can be used for websites, node.js projects and is not reliant on browsers Dom or JavaScript frameworks.
Jasmine is a Behavioural Driven Development testing frameworks, it has taken a lot from Rspec. Jasmin tests are primarily split into two parts: the ‘describe’ block and the ‘it’ block.
What is BDD?
Have you heard of TDD?
TDD is where you write your tests first?
So you:
Write failing tests
Watch them fail
Make them pass
Refactor
Repeat
You do this my implementing the minimum you need to get the test to pass at each stage.
BDD is at a different view point in that you
Establish and agree a goal
Use examples to describe the behaviour of the code
Automate those examples
So to Jasmine?
describe("A suite", function() { it("contains spec with an expectation", function() { expect(true).toBe(true); });});
What is a Suite?
A suite calls the global Jasmine function ‘describe’ and provides two parameters a strong and a function, where the string is the name of a spec suite (i.e. what is being tested), the function is a block of code that implements the suite.
What is a Spec?
Specs are defined by calling the global function it, which takes a string and a function, where the string is the name of the test and the
A Suite represents a bunch of tests that are related. Each suite in turn contains a set of Expectations that compare the results of the test - called the actual - with the expected value. A Suite is defined by calling the describe() function. It takes two parameters: the name of the Suite, and the function which contains the calls to the expectation methods called Specs. These are defined using the it() method. Like describe(), it() also accepts a name and function parameter. The it() function parameter may contain variables and one or more calls to the expect() method. Used in conjunction with a Matcher function, these carry out the task of comparing the actual and expected values.
Add matchers to earlier slide
Both describe and it take two parameters a text string and a function
The string that you pass to describe and it should form a sentence as such and help the test to be readable by business people.
Inside the it you can have all the code you need for your test.
The expect part is the part that you are testing, The value you give to expect is what gets tested using the matcher that you specify.
JavaScript scoping rules apply so anything declared in the describe is available in the it.
Some of the matchers you are more likely to need.
Just like with most testing you want tests to be self contained, meaning that you reset the state before and after each test.
You don’t want to be doing this in every it call so you can use the beforeEach()
Another way to share variables between a before each, it and afterEach is through the THIS keyword. Each spec’s beforeEach/it/afterEach has the thies as the same empty object that is set back to empty for the next spec’s beforeeach/it/afterEach.
Use if set up and teardown are expensive and they are run before all the specs in Describe are run and then after all specs are finished.
Because they are not reset between specs they could leak state between specs.
A custom matcher takes an actual and expected value.
Jasmine has double functions called spies. A spy can stub a ny function and tracks calls to it nd all arguments. A spy only exists in the describe or it block it is defined and will be removed after each spec. There are special matchers for interacting with spies.
Since version 2.0 toHaveBeenCalledWith will return true if the argument list matches any of the recorded calls to the spy.
Jasmine has double functions called spies. A spy can stub a ny function and tracks calls to it nd all arguments. A spy only exists in the describe or it block it is defined and will be removed after each spec. There are special matchers for interacting with spies.
Since version 2.0 toHaveBeenCalledWith will return true if the argument list matches any of the recorded calls to the spy.
Jasmine has double functions called spies. A spy can stub any function and tracks calls to it and all arguments. A spy only exists in the describe or it block it is defined and will be removed after each spec. There are special matchers for interacting with spies.
Since version 2.0 toHaveBeenCalledWith will return true if the argument list matches any of the recorded calls to the spy.
This replaces the method call with the fake method. You can then further extend this to return a value.