4

我正在尝试使用茉莉花为环回模型编写单元测试。我的模型具有通常的 CRUD 端点,但我定义了一个自定义的“/products/:id/upload”端点,它需要一个带有文件的表单。

我的模型看起来像

'use strict';

var loopback = require('loopback');

var ProductSchema = {
    location: {
        type: String, 
        required: true
    },
    version: {
        type: String,
        required: true
    },
    id: { type: Number, id: 1, generated: true }
};

var opts = {
    strict: true
};

var dataSource = loopback.createDataSource({
    connector: loopback.Memory
});
var Product = dataSource.createModel('Product', ProductSchema, opts);


Product.beforeRemote('upload', function(ctx){
    var uploader = function(req, res){
        // parse a multipart form
        res({
            result:'success'
        });
    };
    function createProduct(uploaderResult){
        // create a product out of the uploaded file
        ctx.res.send({
            result: uploaderResult.result
        });
    }
    uploader.upload(ctx.req, createProduct);
});

Product.upload = function () {
    // empty function - all the logic takes place inside before remote
};

loopback.remoteMethod(
    Product.upload,
    {
        accepts : [{arg: 'uploadedFiles', http: function(ctx){
                        return function() {
                            return { files : ctx.req.body.uploadedFiles, context : ctx };
                        };
                    }},
                   {arg: 'id', type: 'string'}],
        returns : {arg: 'upload_result', type: String},
        http: {path:'/:id/upload', verb: 'post'}
    }
);

module.exports = Product;

我的最终目标是测试“createProduct”的逻辑。我的测试看起来像

'use strict';

describe('Product Model', function(){
    var app = require('../../app');
    var loopback = require('loopback');
    var ProductModel;
    beforeEach(function(){
        app = loopback();
        app.boot(__dirname+'/../../'); // contains a 'models' folder
        ProductModel = loopback.getModel('Product');
        var dataSource = loopback.createDataSource({
            connector: loopback.Memory
        });

        ProductModel.attachTo(dataSource);
    });

    it('should load file ', function(){
        console.log(ProductModel.beforeRemote.toString());
        console.log(ProductModel);
        ProductModel.upload();
    });
});

通过调用 ProductModel.upload(); 我希望触发之前的远程挂钩,这将执行 createProduct。我可以单独测试“createProduct”,但是我会忽略 createProduct 作为上传结果最终被调用的事实。

非常清楚,核心问题是:如何在 unittests 中使用远程方法挂钩?

4

1 回答 1

2

建议使用supertest作为 http 服务器。下面是一个代码片段,说明了如何在 jasmine 中执行此操作

describe('My product suite', function(){
    var request = require('supertest');
    var app;

    beforeEach(function(){
        app = loopback();
        // don't forget to add REST to the app
        app.use(app.rest());
    });

    it('should load file', function() {
      request(app).post('/products/id-of-existing-product/upload')
        .attach('file', 'path/to/local/file/to/upload.png')
        .expect(200)
        .end(function(err, res) {
          if (err) return done(err);
          // res is the HTTP response
          // you can assert on res.body, etc.
        });
    });
});
于 2014-06-28T10:00:13.533 回答