环回测试上载文件为具有角色

时间:2015-12-03 12:08:33

标签: node.js loopbackjs strongloop

我一直在使用loopback-testing项目为我的loopback后端编写测试。 后端设置了loopback-component-storage,以便为文件系统中的文件存储提供api。我想使用loopback-component-storage提供的远程api来测试文件上传:

describe('Containers', function() {
   lt.it.shouldBeAllowedWhenCalledByUserWithRole(TEST_USER, someRole, 
      'POST', '/api/containers/somecontainer/upload', somefile);
});

但没有运气......没有关于此的文件。我不知道是否有可能进行测试。有什么想法吗?

提前致谢

一些链接:

https://github.com/strongloop/loopback-testing

https://github.com/strongloop/loopback-component-storage

1 个答案:

答案 0 :(得分:0)

loopback-testing is currently deprecated.

您应该考虑使用supertest。它依赖于superagent,允许您在REST API上执行http请求并在响应对象上进行断言。

然后,您可以使用attach method of super-agent构建可包含文件的多部分表单数据请求。

使用mocha描述测试外观的代码如下:

var request = require('supertest');
var fs = require('fs');
var app = require('./setup-test-server-for-test.js');

function json(verb, url) {
    return request(app)[verb](url)
    .set('Content-Type', 'multipart/form-data');
};

describe("User",function() {
    it("should be able to add an asset to the new project", function(done){
       var req = json('post', '/api/containers/someContainer/upload?access_token=' + accessToken)
       .attach("testfile","path/to/your/file.jpg")
       .expect(200)
       .end(function(err, res){
            if (err) return done(err);
            done();
       });
    });

    it("should have uploaded the new asset to the project folder", function(done){
        fs.access('/path/to/your/file.jpg', fs.F_OK, function(err){
            if (err) return done(err);
            done();
        });
    });
};