15

API エンドポイントをsupertestでテストしていますが、うまく機能しますが、ファイルのダウンロードが成功したかどうかをテストする方法がわかりません。

私のルートファイルでは、エンドポイントを次のように定義しました:

app.get('/api/attachment/:id/file', attachment.getFile);

関数は次のgetFile()ようになります。

exports.getFile = function(req, res, next) {
    Attachment.getById(req.params.id, function(err, att) {
        [...]
        if (att) {
            console.log('File found!');
            return res.download(att.getPath(), att.name);
        }

次に、テスト ファイルで次のことを試します。

describe('when trying to download file', function() {
    it('should respond with "200 OK"', function(done) {
        request(url)
        .get('/api/attachment/' + attachment._id + '/file');
        .expect(200)
        .end(function(err, res) {
            if (err) {
                return done(err);
            }
            return done();
        });
    });
});

ログアウトするので、ファイルが見つかったことは確かですFile found!。手動で試しても問題なく動作しますが、何らかの理由で mocha が返されますError: expected 200 "OK", got 404 "Not Found"

さまざまな MIME タイプと supertest を試しまし.set("Accept-Encoding": "*")たが、何も機能しません。

誰でもこれを行う方法を知っていますか?

4

2 に答える 2

3

ライブラリで問題が修正されたか、コードの他の部分にバグがあります。あなたの例はうまく動作し、

  when trying to download file
File found!
    ✓ should respond with "200 OK"
于 2016-02-14T23:09:08.980 に答える