2016-06-10 31 views
1

Das Objekt db stammt aus dem Modul cloudant.Versucht, den Abruf der Datei durch Spotten der Rohrfunktion zu testen

Dies ist der Code, den ich zu Test versuchen:

res.set('some-header', 'some-value'); 
res.status(200); 

db.attachment.get('some-uuid', 'file'). 
    on('error', function(e) { 
    console.log('err'); 
    reject(e); 
    }). 
    pipe(base64.decode()).pipe(_res).on('error', function(e) { 
    console.log('err2'); 
    reject(e); 
    }); 

fulfill(null); 

Der Versuch, den Code oben zu verspotten:

var sinon = require('sinon'); 
var request = require('supertest'); 

var attachment_1 = { 
    get: sinon.stub(), 
}; 
var db = { 
    attachment: attachment_1 
}; 

var obj = {}; 
var obj2 = {}; 
var sample = {}; 
obj.pipe = function(encoderfunction) { 
    console.log('obj.pipe for encoding'); 
    return obj2; 
}; 
obj2.pipe = function(res) { 
    console.log('obj2.pipe'); 
    console.log(typeof res); // object 
    return this; 
}; 
obj2.on = function() { 
    console.log('obj2'); 
}; 
sample.on = function() { 
    console.log('sample.on'); 
    return obj; 
}; 

db.attachment.get.withArgs('some-uuid', 'file').returns(sample); 

Dies ist der eigentliche Test:

it('should respond with file contents and status of 200', function(done) { 
    request(app).get('/file/index.html') 
     .expect(200) 
     .end(function(err, res){ 
     if (err) { 
      console.log(err); 
     } 
     console.log('res: ' + res); 
     done(); 
    }); 
}); 

Aber Ich erhalte diesen Fehler:

1) should respond with file contents and status of 200: 
    Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test. 
+0

Ist das nicht genug zu tun '.expect (200, getan);'? –

Antwort

0

Aus dem supertest docs:

If you are using the .end() method .expect() assertions that fail will not throw - they will return the assertion as an error to the .end() callback. In order to fail the test case, you will need to rethrow or pass err to done(), as follows:

describe('GET /users', function() { 
    it('respond with json', function(done) { 
    request(app) 
     .get('/users') 
     .set('Accept', 'application/json') 
     .expect(200) 
     .end(function(err, res) { 
     if (err) return done(err); 
     done(); 
     }); 
    }); 
}); 
+0

versuchte dies und es machte keinen Unterschied. – bobbyrne01