Skip to content

Commit

Permalink
Going with a more sensible node.js scheme. And updating to version 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
DouglasMeyer committed Feb 9, 2011
1 parent f6b7e55 commit e8ad62f
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 24 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
.*.swp
Readme.html
3 changes: 3 additions & 0 deletions CHANGELOG
@@ -0,0 +1,3 @@
1.0 (Feb 12th, 2011)

* Initial Release! Designed to work with TestIt 1.2.0
40 changes: 40 additions & 0 deletions Readme.md
@@ -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.
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
0.1
1.0
2 changes: 1 addition & 1 deletion lib/test_it
12 changes: 9 additions & 3 deletions src/mock_it.js
@@ -1,6 +1,6 @@
(function(global){
(function(){

var M = global.MockIt = {
var M = {
'before each': function(t){
t.mocks = [];
t.mock = function(object, functionName){
Expand Down Expand Up @@ -39,4 +39,10 @@
}
};

})(typeof window === 'undefined' ? exports : window);
if (typeof module !== 'undefined'){
module.exports = M;
} else {
window.MockIt = M;
}

})();
35 changes: 16 additions & 19 deletions tests/all_tests.js
@@ -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 []; }
Expand All @@ -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';
Expand All @@ -47,7 +44,7 @@ fakeTestItReporter.prototype.constructor = fakeTestItReporter;
document.getElementById();
document.getElementById();
}
}, MockIt, fakeTestItReporter);
}, MockIt, callback);

},
'should run mock function': function(t){
Expand All @@ -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);
})();
Expand Down

0 comments on commit e8ad62f

Please sign in to comment.