Skip to content

Commit

Permalink
Initial working commit of MockIt
Browse files Browse the repository at this point in the history
  • Loading branch information
Douglas Meyer committed Dec 17, 2010
0 parents commit 2f6367d
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
@@ -0,0 +1,3 @@
[submodule "lib/test_it"]
path = lib/test_it
url = git@github.com:DouglasMeyer/test_it.git
1 change: 1 addition & 0 deletions VERSION
@@ -0,0 +1 @@
0.1
1 change: 1 addition & 0 deletions lib/test_it
Submodule test_it added at 42545b
42 changes: 42 additions & 0 deletions src/mock_it.js
@@ -0,0 +1,42 @@
(function(global){

var M = window.MockIt = {
'before each': function(t){
M.mocks = [];
t.mock = function(object, functionName){
var options = Array.prototype.slice.call(arguments, 2),
mockFunction = options.pop(),
expectedCalls = options.pop();
var mock = {
object: object, functionName: functionName,
originalFunction: object[functionName],
callCount: 0
};
if (expectedCalls){
mock.expectedCalls = expectedCalls;
}
M.mocks.push(mock);
object[functionName] = function(){
mock.callCount += 1;
return mockFunction.apply(this, arguments);
};
}
},
'after each': function(t){
var mock;
try {
for(var i=0;mock=M.mocks[i];i++){
if (mock.expectedCalls){
t.assertEqual(mock.expectedCalls, mock.callCount,
'expected "'+mock.functionName+'" to be called '+mock.expectedCalls+' times, but was called '+mock.callCount+' times');
}
}
} finally {
while(mock = M.mocks.pop()){
mock.object[mock.functionName] = mock.originalFunction;
}
}
}
};

})(window);
70 changes: 70 additions & 0 deletions tests/run.html
@@ -0,0 +1,70 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<style>
#test-it-results { list-style-type: none; margin: 0; padding: 0; }
#test-it-results .summary { cursor: pointer; padding: 4px; border-width: 2px; border-style: solid; }
#test-it-results li { margin: 2px 0 0 0; padding: 1px; display: none; }
#test-it-results.show-passing li { display: block; }
#test-it-results .summary { font-size: 150%; display: block; }
#test-it-results .running { background-color: #AAAAAA; border-color: #727272; display: block; }
#test-it-results .pass { background-color: #E4FFE4; border-color: #98AA98; }
#test-it-results .fail { background-color: #FFB0B0; border-color: #AA7676; display: block; }
#test-it-results .error { background-color: #FF4040; border-color: #AA2B2B; display: block; }
</style>
<script src="../lib/test_it/src/test_it.js"></script>
<script src="../src/mock_it.js"></script>
<script>
var log,
fakeTestItReporter = function(results){
log = this.constructor.log = [];
log.appendChild = log.push;
this.reportContext(results);
return results;
};

fakeTestItReporter.prototype = new TestIt.Reporter({});
fakeTestItReporter.prototype.constructor = fakeTestItReporter;
(function(){
var results,
mockedFunctionResponse,
wasMockedFunction,
origDocumentGetElementById = document.getElementById;
TestIt('mocking', {
'before all': function(){

results = TestIt('tests', {
'mock test': function(t){
t.mock(document, 'getElementById', function(){
return 'ok';
});
mockedFunctionResponse = document.getElementById('hi');
},
'not mock test': function(t){
wasMockedFunction = document.getElementById;
},
'mock expectedCalls test': function(t){
t.mock(document, 'getElementById', 1, function(){});
document.getElementById();
document.getElementById();
}
}, MockIt, fakeTestItReporter);

},
'should run mock function': function(t){
t.assertEqual('ok', mockedFunctionResponse);
},
'should return mocked function to original state after each test': function(t){
t.assertEqual(origDocumentGetElementById, wasMockedFunction);
},
'should fail if expectedCalls isn\'t met': function(t){
t.assertEqual('fail', results['tests']['mock expectedCalls test'].result);
t.assertEqual('expected "getElementById" to be called 1 times, but was called 2 times', results['tests']['mock expectedCalls test'].message);
}
}, MockIt);
})();
</script>
</head>
<body></body>
</html>

0 comments on commit 2f6367d

Please sign in to comment.