Trong ví dụ sau, tôi muốn stub chức năng get
để ngăn yêu cầu HTTP thực tế xảy ra. Tôi muốn spy on phương thức get
để kiểm tra xem nó đã được gọi với đối số nào.Sử dụng SinonJS để khai thác và gián điệp trên cùng một chức năng?
var request = require('request'), sinon = require('sinon');
describe('my-lib', function() {
sinon.stub(request, 'get').yield(null, null, "{}");
var spy = sinon.spy(request, 'get');
it('should GET some data', function(done) {
function_under_test(function(err, response) {
if(error) return done(error);
assert(request.get.called);
assert(request.get.calledWith('some', 'expected', 'args'));
});
});
});
Dù sao, Sinon dường như không cho phép theo dõi và theo dõi cùng một phương pháp. Ví dụ trên cho các lỗi sau:
TypeError: Attempted to wrap get which is already wrapped
Làm thế nào để do thám trên một phương pháp, trong khi ngăn chặn hành vi mặc định?
Cảm ơn bạn đã bỏ phiếu ẩn danh! – Armand