Permalink
Browse files

Finalize the merge of mockingbird into the library.

  • Loading branch information...
2 parents 95f49ee + 7c3a896 commit 4943c7ea5597cecab09289ca55d3aff88524cbc5 Curtis Schlak committed Jan 29, 2014
Showing with 277 additions and 96 deletions.
  1. +35 −0 Gruntfile.js
  2. +95 −30 README.md
  3. +47 −18 lib/nodemock.js
  4. +0 −18 lib/npm-debug.log
  5. +55 −25 package.json
  6. +0 −5 test/index.html
  7. +45 −0 test/nodemock.js
View
@@ -0,0 +1,35 @@
+module.exports = function(grunt) {
+
+ // Project configuration.
+ grunt.initConfig({
+ pkg: grunt.file.readJSON('package.json'),
+
+ jshint: {
+ // define the files to lint
+ files: ['./lib/**/*.js'],
+ // configure JSHint (documented at http://www.jshint.com/docs/)
+ options: {
+ // more options here if you want to override JSHint defaults
+ globals: {
+ console: true,
+ module: true
+ }
+ }
+ },
+
+ nodeunit: {
+ all: ['test/*.js']
+ }
+ });
+
+ // load our plugins
+ grunt.loadNpmTasks('grunt-contrib-jshint');
+ grunt.loadNpmTasks("grunt-contrib-nodeunit");
+
+ // and define our tasks
+ // this would be run by typing "grunt test" on the command line
+ grunt.registerTask('test', ['nodeunit']);
+
+ // the default task can be run just by typing "grunt" on the command line
+ grunt.registerTask('default', ['jshint', 'nodeunit']);
+};
View
@@ -1,9 +1,12 @@
-Node Mock - Simple Yet Powerful Mocking Framework for NodeJs
-============================================================
+A simple Yet Powerful Mocking Framework for NodeJs
+==================================================
-NodeMock is a very simple to use mocking framework which can be used to
-mock functions in JavaScript objects.
-NodeMock creates mock methods in less code with more expressive manner.
+[NodeMock](https://github.com/realistschuckle/nodemock) is a Simple, Yet
+Powerful Mocking Framework for NodeJs
+
+NodeMock is a very simple to use mocking framework which can be used to mock
+functions in JavaScript objects. NodeMock creates mock methods in less code
+with more expressive manner.
Features
--------
@@ -20,11 +23,14 @@ Besides it's simplicity it supports following features:
* Fail support added when calling method that should not be called
* Mock support to call a single method more than once
* Repetitive support
-* ignore methods from mocking behaviour
+* Ignore methods from mocking behaviour
+* Ability to provide functions to determine valid input for mocked functions
+* Ability to provide functions to generate output from mocked functions
Testing
-------
-Node JS can be used with any testing framework. And we've used it with Nodeunit and it's a perfect match.
+Node JS can be used with any testing framework. And we've used it with Nodeunit
+and it's a perfect match.
[See Examples](https://github.com/realistschuckle/nodemock/blob/master/test/nodemock.js "Nodemock with Nodeunit")
Install
@@ -37,30 +43,72 @@ Usage
------
### Load the Module
- var nodemock = require("nodemock");
+ var nodemock = require("nodemock");
### Creating a mock function with taking arguments and return value
- var mocked = nodemock.mock("foo").takes(10, [10, 20, 30]).returns(98);
-
- mocked.foo(10, [10, 20, 30]); // this will return 98
-
- mocked.foo(10); //throws execption
+ var mocked = nodemock.mock("foo").takes(10, [10, 20, 30]).returns(98);
+
+ mocked.foo(10, [10, 20, 30]); // this will return 98
+
+ mocked.foo(10); //throws execption
### Creating a mock with callback support
- var mocked = nodemock.mock("foo").takes(20, function(){}).calls(1, [30, 40]);
+ var mocked = nodemock.mock("foo")
+ .takes(20, function(){})
+ .calls(1, [30, 40]);
mocked.foo(20, function(num, arr) {
console.log(num); //prints 30
console.log(arr); //prints 40
});
/*
- When you invoke foo() nodemock will calls the callback(sits in argument index 1 - as specified)
- with the parameters 30 and 40 respectively.
+ When you invoke foo() nodemock will calls the callback(sits in argument
+ index 1 - as specified) with the parameters 30 and 40 respectively.
+ */
+
+
+### Creating a mock function that takes variable parameters
+
+ var mocked = nodemock.mock("foo").takes(function(args) {
+ return(args % 2 == 0)
+ });
+ mocked.foo(4) // works
+
+ mocked.foo(5) // fails
+
+### Creating a mock function that takes variable parameters and returns dynamic values
+
+ var mocked = nodemock.mock("foo").takesF(function(args) {
+ return(args % 2 == 0)
+ }).returnsF(function(args) {
+ return(args * 2)
+ });
+ mocked.foo(4) // returns 8
+
+ mocked.foo(5) // fails
+
+### Creating a mock with callback support
+
+ var mocked = nodemock.mock("foo")
+ .takes(20, function(){})
+ .calls(1, [30, 40]);
+
+ mocked.foo(20, function(num, arr) {
+ console.log(num); //prints 30
+ console.log(arr); //prints 40
+ });
+
+ /*
+ When you invoke foo() nodemock will calls the callback (sits in argument
+ index 1 - as specified) with the parameters 30 and 40 respectively.
*/
### Controlling callbacks
-With the asynchronous nature of NodeJS(and brower with AJAX too) it'll be great if we can control the execution of the callback in the testing environment. And `ctrl()` of nodemock helps that
+
+With the asynchronous nature of NodeJS(and brower with AJAX too) it'll be great
+if we can control the execution of the callback in the testing environment. And
+`ctrl()` of nodemock helps that
var ctrl = {};
var mocked = nodemock.mock('foo').takes(10, function() {}).ctrl(1, ctrl);
@@ -125,7 +173,7 @@ With the asynchronous nature of NodeJS(and brower with AJAX too) it'll be great
### reset the mock
- var mocked = nm.mock('foo').returns(100);
+ var mocked = nodemock.mock('foo').returns(100);
mocked.foo(); //returns 100
mocked.assert(); //returns true
@@ -136,15 +184,16 @@ With the asynchronous nature of NodeJS(and brower with AJAX too) it'll be great
mock.assert() //returns true
### ignore method
-Sometime we need to ignore some methods going through mocking rules. But we need to have those methods but doing nothing.
+Sometime we need to ignore some methods going through mocking rules. But we
+need to have those methods but doing nothing.
- var mocked = mock.ignore('hello');
+ var mocked = nodemock.ignore('hello');
mocked.mock('foo').returns(100);
mock.foo(); //returns 100
mock.hello(); //do nothing but the method exists
- mock.assert(); // return true, assert have nothing to do with ignored methods
+ mock.assert(); // return true, assert ignores ignored methods
API Documentation
-----------------
@@ -161,30 +210,46 @@ API Documentation
mocked.takes(arg1, args2, ...)
Specify arguments of the function and verify then when calling
+
+ mocked.takesF(function)
+ As opposed to takes(), in which we can only specify static values,
+ takesF() (note the "F" in the name to indicate that it takes a function)
+ allows client code to provide a predicate that determines whether the
+ mocked function should accept ("take") the value and allowing mock code
+ to be a bit more dynamic.
+
+ mocked.takesAll()
+ A shorthand function that tells the mock function to accept any input.
mocked.returns(returnValue)
Specify the return value of the function
-
+
+ mocked.returnsF(function)
+ Similar to takesF(), returnsF() allows to provide a function that will
+ dynamically generate values that will be returned from mock calls. The
+ function will be provided with the parameters that were passed to the
+ mock call.
+
mocked.calls(callbackPosition, argumentsArray)
Calls a callback at the arguments in index `callbackPosition`
with the arguments specified in the "argumentsArray"
- when using this you've to define a function signature as a callback in the argument list
- for a callback at index 2 .takes() function will be as,
+ when using this you've to define a function signature as a callback in
+ the argument list for a callback at index 2 .takes() function will be as,
mocked.takes(10, 20, function(){})
mocked.fail()
- If calls at very begining afterword any call on the mocked objects will fail
- Otherwise current mock method will fails someone called that.
+ If calls at very begining afterword any call on the mocked objects will
+ fail. Otherwise current mock method will fails someone called that.
mocked.times(repetitiveCount);
- We can rule the mocked method to be called multiple times with same parameters
- Finally we can check that using above assert method;
+ We can rule the mocked method to be called multiple times with same
+ parameters. Finally we can check that using above assert method;
mocked.reset()
- Reset all the rules and mocks created. And bring mocked object into a stage when
- it's created
+ Reset all the rules and mocks created. And bring mocked object into a
+ stage when it's created
mocked.ignore()
Ignore Some methods from the mocking behaviour
Oops, something went wrong.

0 comments on commit 4943c7e

Please sign in to comment.