简体   繁体   English

使用超级,摩卡和模型进行环回测试

[英]Loopback testing with supertest, mocha and models

On the Google groups post on deprecating loopback-testing there is a question that asks about providing a proper example of how testing can be achieved without loopback-testing. 关于弃用环回测试谷歌小组发布的帖子中有一个问题,即提供一个如何在没有环回测试的情况下实现测试的正确示例。 That thread talks about using supertest instead. 该线程谈论使用supertest而不是。

Below is an attempt that I made to combine Mocha, supertest along with models ( from app.js ). 下面是我尝试将Mocha,supertest和模型( 来自app.js )结合起来。 The result works really well when I run the file by itself. 当我自己运行文件时,结果非常好。 But if I had another test file (say test-teacher.js) then the first test file (call it test-student.js) starts to fail in weird ways I can't describe. 但是,如果我有另一个测试文件(比如test-teacher.js),那么第一个测试文件(称为test-student.js)开始以奇怪的方式失败,我无法描述。

Am I missing something or can models not be used like I am using them below? 我错过了什么或者可以使用不能使用的模型,就像我在下面使用它们一样?

describe('/Student', function () {

    var server = require('../server/server')
    var loopback = require('loopback')
    var supertest = require('supertest')
    var request = require('supertest')(server)

    var dataSource = server.dataSource('db', {adapter: 'memory'})

    var Student = dataSource.define('Student', {
        'id': Number,
        'points': Number
    });

    beforeEach(function () {
        Student.updateOrCreate({id: 1, points: 5000});
    })


    it('Post a new student', function (done) {
        request.post('/api/Students').send({points: 5000}).expect(200, done)

    })


})

Based on feedback from jakerella on the previous answer, I changed the code above so that I don't have to redefine the models from scratch in the code (thanks jakerella!) 根据jakerella对前一个答案的反馈,我更改了上面的代码,以便我不必在代码中从头开始重新定义模型(感谢jakerella!)

With the code below, I am able to run all the tests from multiple different models as a single suite using npm test without any failures. 使用下面的代码,我可以使用npm test多个不同模型的所有测试作为单个套件npm test而不会出现任何故障。

Since I am interested in only individual orders ... listen and close were not necessary. 因为我只对个别订单感兴趣...所以不需要倾听和关闭。 I suspect that if I was testing overall instances of models that were created it would become required. 我怀疑,如果我正在测试创建的模型的整体实例,那么它将成为必需。

describe('/Student', function () {

    var server = require('../server/server')
    var request = require('supertest')(server)
    var expect = require('expect.js')

    var Student 

    before(function() {
        Student = server.models.Student    
    })

    beforeEach(function (done) {
        Student.upsert({id: 1, points: 5000}, function() { done() })
    })    

    it('Post a new student', function (done) {
        request.post('/api/Students').send({points: 5000}).expect(200, done)
     })
})

Wanted to toss this into an answer... the first issue was an undefined dataSource var, but then you also had redefined Student in your two tests. 想把它扔进答案......第一个问题是一个未定义的dataSource var,但是你也在你的两个测试中重新定义了Student My recommendation instead is used the LoopBack app and models already defined (usually in common/models/ ).Then the basic implementation for testing (that I use) is something like the code below (using mocha and chai ). 我的建议是使用LoopBack应用程序和已定义的模型(通常在common/models/ )。然后测试的基本实现(我使用的)类似于下面的代码(使用mochachai )。 Note the beforeEach and afterEach to start and stop the server. 请注意beforeEachafterEach以启动和停止服务器。

var assert = require('chai').assert,
    superagent = require('superagent'),
    app = require('../server/server');

describe('Person model', function() {
  var server;

  beforeEach(function(done) {
    server = app.listen(done);
  });

  afterEach(function(done) {
    server.close(done);
  });

  it('should log in and log out with live server', function(done) {
    superagent
      .post('http://localhost:3000/api/People/login')
      .send({ email: 'john@doe.com', password: 'foobar' })
      .set('Accept', 'application/json')
      .set('Content-Type', 'application/json')
      .end(function(err, loginRes) {
        if (err) { return done(err); }

          assert.equal(loginRes.status, 200);
          assert.ok(loginRes.body);
          assert.equal(loginRes.body.userId, 1);
        }
      });
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM