Giter Club home page Giter Club logo

keystonejs-stub's Introduction

keystonejs-stub

Keystonejs-stub is a stubbing system for keystonejs to be used with unit testing frameworks like Jasmine. Using this stubbing system you can test any module that requires keystone. Although Jasmine is used to demonstrate how the stubbing system works it should be testing-framework agnostic. The setup of the testing framework itself is outside the scope of this project.

Usage

Install it:

    npm install keystonejs-stub --save-dev

Require it in your test spec:

    stubKeystone = require('keystonejs-stub'),

Use it--the following example uses proxyquire:

    SampleModule = proxyquire('./SampleModule', {
        'keystone': stubKeystone
    });

For more usage examples please checkout the samples in the "tests" folder. You may need to run "npm install" in the keystonejs-stub directory to install dev dependencies. You can execute the sample tests by running "npm test" under the keystonejs-stub directory.

Notes

  • The intent of this stub system is not for doing integration or end-to-end testing, which can be accomplished in a slightly different manner. The stub system is specifically for unit testing. That assumes that you have adhered as much as possible to the Single Responsibility Principle when designing/implementing your application modules. The stub system heavily relies on mocking stubbed methods that return results, which affect subsequent logic in the module.

  • If you are testing List Models you need to export the list model:

      var keystone = require('keystone');
    
      var SampleListModel = new keystone.List('SampleListModel', {});
    
      SampleListModel.add({name: { type: String } });
    
      SampleListModel.register();
    
      module.exports = SampleListModel;
    
  • Regarding mocking and assuming you have the following chained mongoose call:

      SomeList.model.findOne({'slug': 'xxx'}).sort('-yyy').populate('zzz').exec(function (err, item) {
          ...
      });
    

    Then you can mock that chain in various ways:

    • You can assume keystonejs-stub's default mocking for .sort() and .populate() and just mock the .exec() as follows:

        spyOn(SomeList.model,'exec').and.callFake(function(callback){
            callback && callback(null, <result listing data>);
        });
      
    • You can mock either .sort() or .populate() along with .exec() as follows:

        spyOn(stubKeystone.lists['SampleListModel'].model,'find').and.returnValue({
            sort: function (arg) {
                <do-whatever-sort>
                return {
                    exec: function (callback) {
                        <do-whatever-before-callback>
                        callback && callback(null, <result listing data>);
                    }
                }
            }
        });
      
    • The previous mock can also be done as independent mocks as follows:

        spyOn(stubKeystone.lists['SampleListModel'].model,'sort').and.callFake(function(arg){
            <do-whatever-sort>
            return this;
        });
      
        spyOn(stubKeystone.lists['SampleListModel'].model,'exec').and.callFake(function(callback){
            <do-whatever-before-callback>
            callback && callback(null, <result listing data>);
        });
      
    • You can mock all .sort(), .populate(), and .exec() as follows:

        spyOn(stubKeystone.lists['SampleListModel'].model,'find').and.returnValue({
            sort: function (arg) {
                <do-whatever-sort>
                return {
                    populate: function (arg) {
                        <do-whatever-populate>
                        return {
                            exec: function (callback) {
                                <do-whatever-before-callback>
                                callback && callback(null, <result listing data>);
                            }
                        }
                    }
                }
            }
        });
      
    • or you can break the above as independent mocks as previously described

  • When mocking models you can just assign them directly to the lists object in the keystone stub:

      stubKeystone.lists['SampleListModel'] = proxyquire('./SampleListModel', {
          'keystone': stubKeystone
      });
    

    NOTE: However, if the model is the SUT then you don't need to assign it to lists!

  • To test list virtuals, methods, and statics you must first set a document item in the list:

      SampleListModel.setDoc(doc);
    

If you need to enable logging on keystonejs-stub then set (windows) or export (linux) any of the following debug tags:

keystone
list
model
schema

For example:

export DEBUG=keystone,list,schema,model && npm test

This is work-in-progress and based on current needs. Feel free to send improvements!

License

MIT. Copyright (c) 2015 Carlos Colon (webteckie)

keystonejs-stub's People

Contributors

mnyon-grandkru avatar gitter-badger avatar

Watchers

James Cloos avatar Simon Knox avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.