2015-01-30 5 views
12

के साथ कोड के लिए जेस्ट के साथ यूनिट परीक्षण कैसे लिखें I नीचे दिए गए कोड के लिए जेस्ट और जैस्मीन-गड्ढे के साथ एक यूनिट परीक्षण लिखने की कोशिश कर रहा हूं और पूरी तरह से इसके साथ फंस गया हूं। कोड एक AJAX कॉल है जो संसाधन से कुछ डेटा पुनर्प्राप्त करता है और इसे चर में सहेजता है।वचन

init = function() { 
    var deferred = Q.defer(); 
    $.ajax({ 
     type: 'GET', 
     datatype: 'json', 
     url: window.location.origin + name, 
     success: function (data) { 
      userId = data.userId; 
      apiKey = data.apiKey; 
      deferred.resolve(); 
     } 
    }); 
    return deferred.promise; 
}, 
+0

असंबंधित टिप्पणी (आप पहले से ही अपने जवाब है): [आस्थगित है एक विरोधी पैटर्न] (https://github.com/petkaantonov/bluebird/wiki/Promise-anti-patterns), नहीं है उन्हें इस्तेमाल करें :)। –

उत्तर

9

इससे मुझे आज का अधिकांश दिन निराश हो गया। यहां मैंने जो समाप्त किया है (मेरे एक्शन क्रिएटर (फ्लक्स) का परीक्षण करना जो एक एपीआई का उपयोग करता है जो वादे के आधार पर वादे देता है और सामान भेजता है)। असल में मैं एपीआई विधि का मज़ाक उड़ाता हूं जो वादा करता है और इसे तुरंत हल करता है। आपको लगता है कि यह .then (...) आग लगने के तरीकों को प्राप्त करने के लिए पर्याप्त होगा, लेकिन पिट कोड को मेरे एक्शन क्रिएटर को वास्तव में हल किए गए वादे के आधार पर काम करने की आवश्यकता थी।

jest.dontMock('../LoginActionCreators.js'); 
jest.dontMock('rsvp'); //has to be above the require statement 

var RSVP = require('rsvp'); //could be other promise library 

describe('LoginActionCreator', function() { 
    pit('login: should call the login API', function() { 
    var loginActionCreator = require('../LoginActionCreators'); 
    var Dispatcher = require('../../dispatcher/Dispatcher'); 
    var userAPI = require('../../api/User'); 
    var Constants = require('../../constants/Constants'); 

    //my api method needs to return this 
    var successResponse = { body: {"auth_token":"Ve25Mk3JzZwep6AF7EBw=="} }; 

    //mock out the API method and resolve the promise right away 
    var apiMock = jest.genMockFunction().mockImplementation(function() { 
     var promise = new RSVP.Promise(function(resolve, reject) { 
     resolve(successResponse); 
     }); 

     return promise; 
    }); 
    //my action creator will dispatch stuff based on the promise resolution, so let's mock that out too 
    var dispatcherMock = jest.genMockFunction(); 

    userAPI.login = apiMock; 
    Dispatcher.dispatch = dispatcherMock; 

    var creds = { 
     username: 'username', 
     password: 'password' 
    }; 

    //call the ActionCreator 
    loginActionCreator.login(creds.username, creds.password); 

    //the pit code seems to manage promises at a slightly higher level than I could get to on my 
    // own, the whole pit() and the below return statement seem like they shouldnt be necessary 
    // since the promise is already resolved in the mock when it is returned, but 
    // I could not get this to work without pit. 
    return (new RSVP.Promise(function(resolve) { resolve(); })).then(function() { 
     expect(apiMock).toBeCalledWith(creds); 
     expect(dispatcherMock.mock.calls.length).toBe(2); 
     expect(dispatcherMock.mock.calls[0][0]).toEqual({ actionType: Constants.api.user.LOGIN, queryParams: creds, response: Constants.request.PENDING}); 
     expect(dispatcherMock.mock.calls[1][0]).toEqual({ actionType: Constants.api.user.LOGIN, queryParams: creds, response: successResponse}); 
    }); 
    }); 
}); 

यहाँ ActionCreator जो डिस्पैचर करने के लिए एपीआई बाँधती है:

'use strict'; 

var Dispatcher = require('../dispatcher/Dispatcher'); 
var Constants = require('../constants/Constants'); 
var UserAPI = require('../api/User'); 


function dispatch(key, response, params) { 
    var payload = {actionType: key, response: response}; 
    if (params) { 
    payload.queryParams = params; 
    } 
    Dispatcher.dispatch(payload); 
} 

var LoginActionCreators = { 

    login: function(username, password) { 
    var params = { 
     username: username, 
     password: password 
    }; 

    dispatch(Constants.api.user.LOGIN, Constants.request.PENDING, params); 

    var promise = UserAPI.login(params); 

    promise.then(function(res) { 
     dispatch(Constants.api.user.LOGIN, res, params); 
    }, function(err) { 
     dispatch(Constants.api.user.LOGIN, Constants.request.ERROR, params); 
    }); 
    } 
}; 

module.exports = LoginActionCreators; 
0

जेस्ट की वेबसाइट पर इस ट्यूटोरियल सवाल सीधे उत्तर नहीं मिलता है, लेकिन कैसे इकाई परीक्षण का सार है पक्का वादा।

https://facebook.github.io/jest/docs/tutorial-async.html

संबंधित मुद्दे