Permalink
Browse files

Going with a more sensible node.js scheme. And updating to version 1.0

  • Loading branch information...
DouglasMeyer committed Feb 9, 2011
1 parent f6b7e55 commit e8ad62f811fce71b45047cec7e896bfb65e490b6
Showing with 72 additions and 24 deletions.
  1. +2 −0 .gitignore
  2. +3 −0 CHANGELOG
  3. +40 −0 Readme.md
  4. +1 −1 VERSION
  5. +1 −1 lib/test_it
  6. +9 −3 src/mock_it.js
  7. +16 −19 tests/all_tests.js
View
@@ -0,0 +1,2 @@
+.*.swp
+Readme.html
View
@@ -0,0 +1,3 @@
+1.0 (Feb 12th, 2011)
+
+* Initial Release! Designed to work with TestIt 1.2.0
View
@@ -0,0 +1,40 @@
+# MockIt
+
+Simple mocking for the TestIt framework.
+
+
+## How do I use it?
+
+ TestIt('new Dice(6).roll()', {
+ 'should return a random value for a 6-sided die': function(t){
+ t.mock(Math, 'random', 1, function(){
+ t.assert(arguments.length === 0, "Don't pass Math.random() arguments!");
+ return 2/6 + .009352; // Math.random() will return this "random" number.
+ });
+ t.assertEqual(2, new Dice(6).roll());
+ }
+ }, MockIt);
+
+And if you are testing in node.js remember to require it `var MockIt = require('./mock_it');` and you'll be good to go. In this example, when `Math.random()` is called it will call our function and return the staged value. `Math.random` will return to its original value after the test is done running.
+
+
+## Arguments
+
+t.mock(object, function name to mock, [optional number of expeced calls], function to be called);
+
+### object, function name to mock
+
+These are basically describing what it is you want to mock. If you want to mock `Math.random()`, then pass `t.mock(Math, 'random', ...`. If you want to mock a prototype's method, like `dice.roll()`, then pass `t.mock(Dice.prototype, 'random', ...`.
+
+### optional number of expected calls
+
+If you want to ensure your mocked function gets called, you can specify how many times it gets called. This is optional, so if you don't care if the mocked function is called, you can skip this: `t.mock(Math, 'random', function(){ returns 0.9; });`.
+
+### function to be called
+
+This is the function to be called instead of the original function. This function will get passed the same arguments that would have been passed to the original function, and what is returned from this function will get returned as though it were returned from the original function. Inside this function could be a good place to run assertions, if that works in your case.
+
+
+## Gotchas
+
+`t.mock` is only available in 'before each', 'after each', and test functions. 'before all', and 'after all' get no love yet.
View
@@ -1 +1 @@
-0.1
+1.0
View
@@ -1,6 +1,6 @@
-(function(global){
+(function(){
- var M = global.MockIt = {
+ var M = {
'before each': function(t){
t.mocks = [];
t.mock = function(object, functionName){
@@ -39,4 +39,10 @@
}
};
-})(typeof window === 'undefined' ? exports : window);
+ if (typeof module !== 'undefined'){
+ module.exports = M;
+ } else {
+ window.MockIt = M;
+ }
+
+})();
View
@@ -1,7 +1,7 @@
var TestIt, MockIt;
if (typeof window === 'undefined') {
- TestIt = require('../lib/test_it/src/test_it').TestIt;
- MockIt = require('../src/mock_it').MockIt;
+ TestIt = require('../lib/test_it/src/test_it');
+ MockIt = require('../src/mock_it');
var element = {
appendChild: function(){},
getElementsByTagName: function(){ return []; }
@@ -14,25 +14,22 @@ if (typeof window === 'undefined') {
};
}
-var log,
- fakeTestItReporter = function(results){
- log = this.constructor.log = [];
- log.appendChild = log.push;
- this.reportContext(results);
- return results;
- };
-
-fakeTestItReporter.prototype = new TestIt.DomReporter({});
-fakeTestItReporter.prototype.constructor = fakeTestItReporter;
(function(){
- var results,
- mockedFunctionResponse,
+ var mockedFunctionResponse,
wasMockedFunction,
- origDocumentGetElementById = document.getElementById;
+ origDocumentGetElementById = document.getElementById,
+ expectedCallsResult,
+ expectedCallsMessage,
+ callback = function(name, result, assertionCount, message){
+ if (name.join(': ') === 'tests: mock expectedCalls test'){
+ expectedCallsResult = result;
+ expectedCallsMessage = message;
+ }
+ };
TestIt('mocking', {
'before all': function(){
- results = TestIt('tests', {
+ TestIt('tests', {
'mock test': function(t){
t.mock(document, 'getElementById', function(){
return 'ok';
@@ -47,7 +44,7 @@ fakeTestItReporter.prototype.constructor = fakeTestItReporter;
document.getElementById();
document.getElementById();
}
- }, MockIt, fakeTestItReporter);
+ }, MockIt, callback);
},
'should run mock function': function(t){
@@ -57,8 +54,8 @@ fakeTestItReporter.prototype.constructor = fakeTestItReporter;
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);
+ t.assertEqual('fail', expectedCallsResult);
+ t.assertEqual('expected "getElementById" to be called 1 times, but was called 2 times', expectedCallsMessage);
}
}, MockIt);
})();

0 comments on commit e8ad62f

Please sign in to comment.