![]() |
source: https://cms-assets.tutsplus.com/uploads/users/487/posts/22836/final_image/http-mock-test.png |
Mục tiêu và tại sao lại giả lập HTTP, và có tác dụng gì cho việc unit test - TDD. Đơn giản là, hiện tại mình đang viết vài project NodeJS theo cơ chế "thông" nhau bởi API. Unit test vốn dĩ là isolated test, có nghĩa là cô lập nó lại mà test. Nên mình cần giả lập (mock) http response từ API server, rồi tiếp đó mình chỉ tập trung viết code cho client là đủ (không thèm biết API nó mần sao, chỉ cần giả lập những response như nó mô tả là đủ)
Cài đặt
npm install nock --save-dev
Đoạn code thực hiện gọi API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports.getDocument = function (key, callback) { | |
var options = { | |
url: 'http://localhost:8080/document', | |
qs: {key: key} | |
}; | |
request.get(options, function (err, response, body) { | |
if (err || response.statusCode !== 200) { | |
err = err || new Error('Cannot access API'); | |
return callback(err); | |
} | |
try { | |
body = JSON.parse(body); | |
return callback(null, body); | |
} catch (e) { | |
return callback(e); | |
} | |
}); | |
}; |
Ở đây, mình dùng module request để thực hiện gọi API (trong code demo thì là localhost:8080) với phương thức GET. Khi response thành công, mình sẽ parse JSON rồi trả kết quả ra bên ngoài. Đoạn code này, mọi người nhìn vào cũng sẽ hiểu mình đóng gói nó thành module trong NodeJS để dễ sử dụng sau này.
Đoạn code thực hiện test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('Module', function () { | |
describe('Get document', function () { | |
before(function (done) { | |
sinon.spy(request, 'get'); | |
done(); | |
}); | |
after(function (done) { | |
request.get.restore(); | |
done(); | |
}); | |
it('should use module Request with method GET', function (done) { | |
var body = JSON.stringify({ | |
error: false, | |
message: '', | |
data: { | |
name: {'Something'} | |
} | |
}); | |
nock.cleanAll(); | |
nock('http://localhost:8080') | |
.get('/document') | |
.query({key: 'test'}) | |
.reply(200, body, {"content-type": 'application/json'}); | |
helper.getDocument('test', function (err, data) { | |
expect(request.get.called).to.be.ok; | |
expect(err).to.be.null; | |
done(); | |
}) | |
}); | |
}); | |
}); |
Ở đây, mình dùng Sinon để theo dõi (spy) xem có đúng là mình dùng module request và gọi method GET hay không. Và mình dùng Nock để giả lập HTTP như đoạn code từ dòng 22 đến dòng 26. Bạn sẽ thấy có dòng nock.cleanAll(); - cái này dùng để xóa sạch những cấu hình mock HTTP (nếu có) trước đó, tránh bị nhầm lẫn và rối việc, khiến việc test của bạn bị sai mà không biết sai từ đâu.
Vậy là xong, cái này cũng dễ làm, nhưng thiếu nó khiến bạn khó mà thực hiện isolated test khi gặp tình huống tương tự. Chia sẻ tí kinh nghiệm, hy vọng hữu ích ^_^
Testing with mocha and chaijs
Sinon - Solved method already wrapped
No comments:
Post a Comment