Socket
Socket
Sign inDemoInstall

nodemock

Package Overview
Dependencies
0
Maintainers
0
Versions
25
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.2.6beta to 0.2.7beta

33

lib/nodemock.js

@@ -74,6 +74,10 @@ /**

var func = arguments[entry.callbackIndex];
if(typeof(func) == "function") {
func.apply(this, entry.callbackArgs || []);
} else {
throw new Error("Expected callback is not defined as callback");
entry.callback = func;
if(entry.callbackArgs) {
if(typeof(func) == "function") {
func.apply(this, entry.callbackArgs || []);
} else {
throw new Error("Expected callback is not defined as callback");
}
}

@@ -104,2 +108,18 @@ }

this.ctrl = function(index, obj) {
var entry = getCurrentEntry();
entry.callbackIndex = index;
entry.callbackArgs = false;
obj.trigger = function() {
if(entry.callback) {
entry.callback.apply(this, arguments);
} else {
throw new Error("There is no callback to control");
}
};
return this;
};
this.calls = function() {

@@ -116,2 +136,4 @@

return this;
} else if(arguments[0] instanceof Object) {
} else {

@@ -156,4 +178,5 @@ throw new Error("First arg of the calls() should be the index of the callback");

args: [],
callback: null,
callbackIndex: null,
callbackParams: null,
callbackArgs: [],
returns: undefined,

@@ -160,0 +183,0 @@ executed: 0,

2

package.json
{
"name": "nodemock",
"version": "0.2.6beta",
"version": "0.2.7beta",
"directories": {

@@ -5,0 +5,0 @@ "lib": "./lib"

@@ -11,5 +11,7 @@ Node Mock - Simple Yet Powerful Mocking Framework for NodeJs

Besides it's simplicity it supports following features
* Does not need an existing object to create the mock
* Verify arguments (we check deeply on objects and arrays to check the validity)
* Allow a return to be sent
* Assertion to check whether all the rules executed
* Callbacks can also be executed with providing arguments

@@ -20,2 +22,4 @@ * Multiple mock functions in one object

* Fail support added when calling method that should not be called
* Mock support to call a single method more than once
* Repetitive support

@@ -26,2 +30,3 @@ Testing

Nodeunit and it's a perfect match.
(https://github.com/arunoda/nodemock/blob/master/test/nodemock.js "Nodemock with Nodeunit")

@@ -57,10 +62,3 @@ Install

*/
### Alter an already created mock function
var mocked = nodemock.mock("foo").takes(10).returns(30);
mocked.foo(10); //gives 30
mocked.mock("foo").takes(10, 20);
mocked.foo(10, 20); //gives 30
### Add multiple mock functions

@@ -73,2 +71,12 @@ var mocked = nodemock.mock("foo").takes(10).returns(30);

### Assertion Support
var mocked = nodemock.mock("foo").takes(20);
var mocked = nodemock.mock("bar").takes(40);
mocked.foo(20);
mocked.bar(40);
//check whether what we've defined is actually executed
mocked.assert(); //returns true
### Fails when calls any method in the mock object

@@ -85,8 +93,33 @@ var mocked = nodemock.fail();

### calls a single mocked method, multiple times
var mocked = nodemock.mock("foo").takes(10, 20).times(2);
mocked.foo(10, 20);
mocked.foo(10, 20);
### mock a single method more than once
var mocked = nodemock.mock("foo").takes(10, 20);
mocked.mock("foo").takes(20, 30);
mocked.mock("foo").takes(500);
mocked.foo(10, 20);
mocked.foo(20, 30)
mocked.foo(500);
//check whether everything has done
mocked.assert(); //returns true
API Documentation
-----------------
=== Construction ===
var mocked = require('nodemock').mock('foo');
Creating a object with mock function "foo"
mocked.mock(methodName)
Used to alter or create a new mock method and add rules to it as usual
=== Rules ===
mocked.takes(arg1, args2, ...)

@@ -107,4 +140,2 @@ Specify arguments of the function and verify then when calling

mocked.mock(methodName)
Used to alter or create a new mock method and add rules to it as usual

@@ -115,2 +146,11 @@ mocked.fail()

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;
=== Confirm ===
mocked.assert();
Checks whether rules we've defined using other methods were executed.
If all the rules were executed return true, otherwise false
License

@@ -117,0 +157,0 @@ -------

@@ -295,1 +295,17 @@ /**

};
exports.testCtrl = function(test) {
test.expect(2);
var ctrl = {};
var mock = nm.mock("foo").takes(10, 20, function() {}).ctrl(2, ctrl);
mock.foo(10, 20, function(bb) {
test.ok(bb = 20);
});
ctrl.trigger(20);
ctrl.trigger(20);
test.done();
};
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc