2015-11-18 13 views
6

Tôi có vấn đề viết bài kiểm tra đơn vị cho các thiết lập sau đây như một tập tin jira.js (trong một mô-đun Node.js):nút js kiểm tra đơn vị: chế giễu yêu cầu phụ thuộc

var rest = require('restler'); // https://www.npmjs.com/package/restler 

module.exports = function (conf) { 
    var exported = {}; 

    exported.getIssue = function (issueId, done) { 
     ... 

     rest.get(uri).on('complete', function(data, response) { 
     ... 
    }; 

    return exported; 
}; 

Bây giờ, tôi muốn viết kiểm tra đơn vị cho hàm getIssue của tôi. 'restler' là một máy khách REST mà qua đó tôi thực hiện các lệnh REST tới API JIRA để nhận được một vấn đề JIRA thông qua mã của tôi.

Vì vậy, để có thể kiểm tra createIssue (..), tôi muốn có thể thử phần còn lại 'var trong các bài kiểm tra đơn vị Jasmine của tôi.

Tôi có thể thử phương pháp này bằng cách nào? Vui lòng cho tôi một số gợi ý để tôi có thể tiếp tục. Tôi đã cố gắng sử dụng rewire nhưng tôi đã thất bại.

Đây là những gì tôi có cho đến nay mà không làm việc (tức là phương pháp getIssue hóa ra được định nghĩa.):

var rewire  = require("rewire"); 
var EventEmitter = require('events').EventEmitter; 
var emitter  = new EventEmitter(); 
var cfg   = require("../../../config.js").Configuration; 
var jiraModule = rewire("../lib/jira")(cfg); 
var sinon  = require("sinon"); 
var should  = require("should"); 

// https://github.com/danwrong/restler 
var restMock = { 
    init : function() { 
     console.log('mock initiated'+JSON.stringify(this)); 

    }, 
    postJson : function (url, data, options) { 
     console.log('[restler] POST url='+url+', data= '+JSON.stringify(data)+ 
     'options='+JSON.stringify(options)); 
     emitter.once('name_of_event',function(data){ 
      console.log('EVent received!'+data); 
     }); 
     emitter.emit('name_of_event', "test"); 
     emitter.emit('name_of_event'); 
     emitter.emit('name_of_event'); 
    }, 
    get : function (url, options) { 
     console.log('[restler] GET url='+url+'options='+JSON.stringify(options)); 
    }, 
    del : function (url, options) { 
     console.log('[restler] DELETE url='+url+'options='+JSON.stringify(options)); 
    }, 
    putJson : function (url, data, options) { 
     console.log('[restler] PUT url='+url+', data= '+JSON.stringify(data)+ 
     'options='+JSON.stringify(options)); 
    } 
}; 

var cfgMock = { 
    "test" : "testing" 
}; 

jiraModule.__set__("rest", restMock); 
jiraModule.__set__("cfg", cfgMock); 

console.log('mod='+JSON.stringify(jiraModule.__get__("rest"))); 

describe("A suite", function() { 
it("contains spec with an expectation", function() { 
    restMock.init(); 
    restMock.postJson(null, null, null); 

console.log(cfg.jira); 

    // the following method turns out to be undefined but when i console.log out the jiraModule, i see the entire code outputted from that file 
    jiraModule.getIssue("SRMAPP-130", function (err, result) { 
     console.log('data= '+JSON.stringify(result)); 
    }); 

    expect(true).toBe(true); 
}); 
}); 

Nếu ai đó có thể hướng dẫn cho tôi về cách chế giễu các 'nghỉ ngơi' đòi hỏi phụ thuộc & đơn vị kiểm tra phương pháp này sẽ rất hữu ích.

Ngoài ra, làm thế nào tôi nên giả mạo 'conf' được chuyển đến module.exports?

nhờ

Trả lời

5

Bạn có thể sử dụng proxyquire hoặc mockery để còn sơ khai/chế giễu sự phụ thuộc.

Trong ví dụ bên dưới, tôi đã sử dụng proxyquire. Hy vọng nó giúp.


/* ./src/index.js */ 
var rest = require('restler'); 

module.exports = function (conf) { 
    var exported = {}; 

    exported.getIssue = function (issueId, done) { 
    var uri = ''; 
    var reqObj = ''; 
    var service = { 
     auth : '' 
    }; 

    rest.postJson(uri, reqObj, service.auth).on('complete', function(data, response) { 
     done(data, response); 
    }); 
    }; 

    return exported; 
}; 

/* ./test/index.js */ 
var proxyquire = require('proxyquire'); 
var assert  = require('chai').assert; 
var restlerStub = { 
    postJson: function() { 
    return { 
     on: function(event, callback) { 
     callback('data', 'response'); 
     } 
    } 
    } 
} 

var index = proxyquire('../src/index', {'restler': restlerStub})(); 

describe('index', function() { 
    it('should return the desired issue', function(done) { 
    var issue = index.getIssue('issueId', function(data, response) { 
     assert.equal(data, 'data'); 
     assert.equal(response, 'response'); 
     done(); 
    }) 
    }); 
}); 

/* ./package.json */ 
{ 
    "scripts": { 
    "test": "mocha" 
    }, 
    "dependencies": { 
    "restler": "^3.4.0" 
    }, 
    "devDependencies": { 
    "chai": "^3.4.1", 
    "mocha": "^2.3.4", 
    "proxyquire": "^1.7.3" 
    } 
} 
+0

Jasmine-nút được gắn thẻ, tại sao bạn đang sử dụng mocha? –

+0

Opps, tôi đã không nhận thấy rằng, tuy nhiên chế giễu phụ thuộc không có gì để làm với các khuôn khổ kiểm tra. – sarbbottam

+0

Xin chào, cảm ơn bạn đã hướng dẫn !! làm thế nào để tôi vượt qua trong conf nếu tôi đang sử dụng proxyquire? Tôi cần phải giả lập conf cũng .... được lấy làm đối số bởi module.exports .. – Rookie

Các vấn đề liên quan