JSSpec Manual.
Contents
Introduction to BDD
See Wikipedia entry
Download and Install
See JSSpec/UserGuide
Defining specifications or behaviors
First you need to create an container html file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ko"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> <title>JSSpec results</title> <link rel="stylesheet" type="text/css" href="JSSpec.css" /> <script type="text/javascript" src="diff_match_patch.js"></script> <script type="text/javascript" src="JSSpec.js"></script> <script type="text/javascript">// <![CDATA[ // Your spec goes here // ]]></script> </head> <body></body> </html>
Write your specifications(or behaviors) on "// Your spec goes here" section above:
describe('Plus operator', {
// Your examples goes here
})
describe('Equality operator', {
// Your examples goes here
})
Describing examples
Write your examples on "// Your examples goes here" section above:
describe('Plus operator', {
'should concatenate two strings': function() {
value_of("Hello " + "World").should_be("Hello World");
},
'should add two numbers': function() {
value_of(1 + 2).should_be(3);
}
})
Using before and after to extract out duplications
before each / after each
describe('Plus operator', {
before_each : function() {hw = "Hello World"},
'should concatenate two strings': function() {
value_of(hw).should_be("Hello World");
}
})
before, before_each, and 'before each' are the same.
before all / after all
Use of before_all and after_all is allowed but not recommended. It should generally be avoided since it may introduce dependencies between your tests.
Using expectations
Should be / not be
Checks equality:
value_of('Hello'.toLowerCase()).should_be('hello');
value_of([1,2,3]).should_be([1,2,3]);
value_of(new Date(1979,03,27)).should_be(new Date(1979,03,27));
value_of(document.body.firstChild).should_be(document.body.firstChild);
value_of([1,2,3]).should_not_be([4,5,6]);
Should be empty / not be empty
Checks if an array is empty:
value_of([]).should_be_empty();
Should be true, false / not be true, false
Checks boolean value:
value_of(1 == 1).should_be_true(); value_of(1 != 1).should_be_false();
Should have / have exactly / have at least / have at most
Checks length of item. Note that should_have() and should_have_exactly() have same meaning:
value_of("Hello").should_have(5, "characters"); // "characters" means "length" property of given string
value_of([1,2,3]).should_have(3, "items"); // "items" means "length" property of given array
value_of({name:'Alan Kang', email:'jania902@gmail.com', accounts:['A', 'B']}).should_have(2, "accounts");
value_of([1,2,3]).should_have_exactly(3, "items");
value_of([1,2,3]).should_have_at_least(2, "items");
value_of([1,2,3]).should_have_at_most(4, "items");
Should include / not include
Checks if an array include expected item or not:
value_of([1,2,3]).should_include(2); value_of([1,2,3]).should_not_include(4);
If type of a target object is an object(instead of array) it checks if the object has expected key or not:
value_of({a: 1, b: 2}).should_include("a");
value_of({a: 1, b: 2}).should_not_include("c");
Should match / not match
Checks if an string matches with given regular expression or not:
value_of("Hello World").should_match(/ello/);
value_of("Hello World").should_not_match(/Bye/);
Should fail
Force failure:
value_of(this).should_fail("TODO");
Should be null / Should not be null
TBD
Should be undefined / Should not be undefined
TBD
Conditional Execution
Conditional Execution is used to filter out examples based on embedded Javascript expression. It is particularly useful for dealing with browser specific behaviors:
'should do something [[JSSpec.Browser.IE6 || JSSpec.Browser.IE7]]': function() {
value_of(...).should_be(...);
},
'should do something [[JSSpec.Browser.FF2 || JSSpec.Browser.FF3]]': function() {
value_of(...).should_be(...);
}
Following is a full list of predefined consts:
- By rendering engine:
- JSSpec.Browser.Trident
- JSSpec.Browser.Webkit
- JSSpec.Browser.Gecko
- JSSpec.Browser.Presto
- By platform:
- JSSpec.Browser.Mac
- JSSpec.Browser.Ubuntu
- JSSpec.Browser.Win
- By browser:
- JSSpec.Browser.IE
- JSSpec.Browser.IE6
- JSSpec.Browser.IE7
- JSSpec.Browser.IE8
- JSSpec.Browser.FF
- JSSpec.Browser.FF2
- JSSpec.Browser.FF3
- JSSpec.Browser.Safari
You can also use your own expression(any valid javascript code snippet which can be evaluated as a boolean value) like this:
'should do something [[navigator.appName === "Microsoft Internet Explorer"]]': function() {
value_of(...).should_be(...);
}