All Projects → mateodelnorte → mockrequire

mateodelnorte / mockrequire

Licence: other
Simple module for mocking required dependencies. Works with any testing suite.

Programming Languages

javascript
184084 projects - #8 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to mockrequire

emock
🐞 下一代C/C++跨平台mock库 (Next generation cross-platform mock library for C/C++)
Stars: ✭ 73 (+329.41%)
Mutual labels:  mock
msw-storybook-addon
Mock API requests in Storybook with Mock Service Worker.
Stars: ✭ 168 (+888.24%)
Mutual labels:  mock
Raccoon
Raccoon is a lightweight response mocking framework that can be easily integrated into the Android UI tests.
Stars: ✭ 47 (+176.47%)
Mutual labels:  mock
jest-launchdarkly-mock
Easily unit test LaunchDarkly feature flagged components with jest
Stars: ✭ 14 (-17.65%)
Mutual labels:  mock
elixir mock
Creates clean, concurrent, inspectable mocks from elixir modules
Stars: ✭ 21 (+23.53%)
Mutual labels:  mock
stub-server
Stub server for REST APIs
Stars: ✭ 14 (-17.65%)
Mutual labels:  mock
webapi
WAI based library for web api
Stars: ✭ 27 (+58.82%)
Mutual labels:  mock
smart-cloud
基于springboot && springcloud的脚手架,支持服务合并部署与拆分部署、接口加解密签名、日志数据 脱敏、接口数据mock、接口文档自动生成、请求幂等校验、接口日志&&sql日志切面打印、分表分库分布式事务、国际化语言等
Stars: ✭ 167 (+882.35%)
Mutual labels:  mock
Retromock
Java library for mocking responses in a Retrofit service.
Stars: ✭ 48 (+182.35%)
Mutual labels:  mock
httpmock
Lightweight HTTP mocking in Go (aka golang)
Stars: ✭ 75 (+341.18%)
Mutual labels:  mock
mountebank-api-php
Working with mountebank api it's easy!
Stars: ✭ 17 (+0%)
Mutual labels:  mock
mswjs.io
Official website and documentation for the Mock Service Worker library.
Stars: ✭ 77 (+352.94%)
Mutual labels:  mock
jsxmock
使用 JSX 来定义 Mock Server
Stars: ✭ 31 (+82.35%)
Mutual labels:  mock
mockafka
A testing DSL for kafka-streams
Stars: ✭ 14 (-17.65%)
Mutual labels:  mock
elasticmock
Python Elasticsearch Mock for test purposes
Stars: ✭ 98 (+476.47%)
Mutual labels:  mock
jest-how-do-i-mock-x
Runnable examples for common testing situations that often prove challenging
Stars: ✭ 63 (+270.59%)
Mutual labels:  mock
ng-apimock
Node plugin that provides the ability to use scenario based api mocking: for local development for protractor testing
Stars: ✭ 102 (+500%)
Mutual labels:  mock
DeepfakeHTTP
DeepfakeHTTP is a web server that uses HTTP dumps as a source for responses.
Stars: ✭ 373 (+2094.12%)
Mutual labels:  mock
mongo mock go example
How to mock MongoDB in Golang
Stars: ✭ 30 (+76.47%)
Mutual labels:  mock
Lol-Mock-API
Mock API Tool
Stars: ✭ 15 (-11.76%)
Mutual labels:  mock

Build Status Greenkeeper badge

MockRequire

Simple module for mocking required dependencies. Works with any testing suite. (https://github.com/mateodelnorte/mockrequire).

Example Usage

Imagine your application uses the following module to onboard a user, in reaction to a recieved event. A service will require the module and call it, passing the event as a parameter. But, in the course of developing your application, you'd like to test the module in isolation from the rest of your code. The module depends on another module, 'my_db_lib'.

How can you test the module in isolation?

var db = require('my_db_lib');

module.exports = function onboardUser (event) {

  // we want to test this function's behavior, but need to isolate the behavior of the module apart from the behavior of it's dependency, the db. 

  db.User.findByEmail(event.email, function (err, user) {
    if(err) throw err;

    user.onboarding = 'complete';

    user.save(function (err) { 
      if(err) throw err; 
    });
  });
};

MockRequire provides a simple means of allowing you to mock dependencies of any module you require(). Want to unit test your code without ever having to hit a database, even though your code is require-ing your datastore modules directly? MockRequire is exactly what you need.

The following example uses 'mocha' and 'should' to create a unit test for the module above. We also use 'mockrequire' to mock out our dependencies, parts of our module we don't care to isolate in our test.

require('mocha'),
require('should');
var mockrequire = require('mockrequire');

var user = (function() {
  return {
    save: function (cb) {
      cb(null);
      this.saved = true;
    }
  };
})();

// instead of require()ing our handler directly, we can mockrequire() it and supply an object containing any child dependencies we would like to mock as well. Here we're mocking my_db_lib

var onboardUser = mockrequire('./handler', {
  'my_db_lib': {
    User: { 
      findByEmail: function (email, cb){
        cb(null, user);
      }
    }
  }
});

// we create our unit tests with mocha

describe('userPaymentComplete()', function(){
  onboardUser({ email: '[email protected]' });
  it('should set onboarding as \'complete\'', function() {
    user.should.have.property('onboarding').equal('complete');
  });
  it('should save user', function() {
    user.should.have.property('saved').equal(true);
  });
});

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].