Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Douglas Meyer
committed
Dec 17, 2010
0 parents
commit 2f6367d
Showing
5 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "lib/test_it"] | ||
path = lib/test_it | ||
url = git@github.com:DouglasMeyer/test_it.git |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0.1 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |