Created
May 21, 2012 02:05
-
-
Save Kevnz/2760270 to your computer and use it in GitHub Desktop.
YUI Test: Good BDD version
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Integration test | |
*/ | |
(function () { | |
var YUITest = this.YUITest || require("yuitest"); | |
YUITest.Node.CLI.XUnit(); | |
var Given = { | |
"I have entered the number": function (number, data) { | |
data.calculator.numbers.push(number); | |
} | |
}; | |
var When = { | |
"I press the button": function (button, data) { | |
data.calculator[button](); | |
} | |
}; | |
var Then = { | |
"I should see the result": function (result, data) { | |
YUITest.Assert.areSame(result, data.calculator.result); | |
} | |
}; | |
var featuresTestSuite = new YUITest.TestSuite({ | |
name: "Product: Calculator", | |
setUp: function (data) { | |
data.calculator = {}; | |
}, | |
tearDown: function (data) { | |
delete data.calculator; | |
} | |
}); | |
featuresTestSuite.add(new YUITest.TestCase({ | |
name: "Feature: Addition", | |
init: function (data) { | |
data.calculator.add = function () { | |
this.result = this.numbers.reduce(function (previousElement, currentElement, index, enumerable) { | |
return previousElement + currentElement; | |
}, 0); | |
}; | |
}, | |
destroy: function (data) { | |
delete data.calculator.add; | |
}, | |
setUp: function (data) { | |
data.calculator.numbers = []; | |
data.calculator.result = 0; | |
}, | |
tearDown: function (data) { | |
delete data.calculator.numbers; | |
delete data.calculator.result; | |
}, | |
"Scenario: Add two numbers": function (data) { | |
Given["I have entered the number"](50, data); | |
Given["I have entered the number"](70, data); | |
When["I press the button"]("add", data); | |
Then["I should see the result"](120, data); | |
}, | |
"Scenario: Add three numbers": function (data) { | |
Given["I have entered the number"](50, data); | |
Given["I have entered the number"](100, data); | |
Given["I have entered the number"](70, data); | |
When["I press the button"]("add", data); | |
Then["I should see the result"](220, data); | |
} | |
})); | |
featuresTestSuite.add(new YUITest.TestCase({ | |
name: "Feature: Multiplication", | |
init: function (data) { | |
data.calculator.multiply = function () { | |
this.result = this.numbers.reduce(function (previousElement, currentElement, index, enumerable) { | |
return previousElement * currentElement; | |
}, 1); | |
}; | |
}, | |
destroy: function (data) { | |
delete data.calculator.add; | |
}, | |
setUp: function (data) { | |
data.calculator.numbers = []; | |
data.calculator.result = 0; | |
}, | |
tearDown: function (data) { | |
delete data.calculator.numbers; | |
delete data.calculator.result; | |
}, | |
"Scenario: Multiply two numbers": function (data) { | |
Given["I have entered the number"](5, data); | |
Given["I have entered the number"](5, data); | |
When["I press the button"]("multiply", data); | |
Then["I should see the result"](25, data); | |
}, | |
"Scenario: Multiply three numbers": function (data) { | |
Given["I have entered the number"](10, data); | |
Given["I have entered the number"](7, data); | |
Given["I have entered the number"](3, data); | |
When["I press the button"]("multiply", data); | |
Then["I should see the result"](210, data); | |
} | |
})); | |
YUITest.TestRunner.add(featuresTestSuite); | |
YUITest.TestRunner.run(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment