使用generator angular-fullstack创建第8个端点时测试失败

问题描述 投票:0回答:1

问题与标题一样奇怪。我有一个项目,我使用生成器angular-fullstack创建,我使用Sequelize(f使用MSSQL ...客户端的要求)连接到MSSQL服务器,一切都运行良好,直到我必须创建第8端点(使用angular-fullstack:端点)。

每次我创建一个端点时,除了PATCH动词集成测试之外,所有测试(使用mocha自动创建和执行)都可以工作,我将消除,因为我根本不使用PATCH。

在我创建了第8个端点(我为其他每个端点做同样的事情)之后,由生成器本身创建的集成测试(单元测试完美地工作)开始失败(不仅仅是端点的测试,而是之前用过的其他测试) ),并且它们随机失败(有时3个失败,有时4个失败,有时它们都有效),这让我想起某种竞争条件(我无法找到)。

发现:

  • POST集成测试“有效”,但结果未显示在数据库中。日志显示发送到数据库的正确SQL命令:INSERT INTO [Findings] ([name],[info],[createdAt],[updatedAt]) OUTPUT INSERTED.* VALUES (N'New Finding',N'This is the brand new finding!!!',N'2018-03-05 22:30:24.000',N'2018-03-05 22:30:24.000');,它返回201作为状态。
  • 当返回的状态代码是500时,错误通常是name: 'SequelizeDatabaseError', message: 'Invalid object name \'Findings\'.',,好像表不存在,但它确实存在!

如果您对我能尝试什么有所了解,我将不胜感激! (我已经搜遍了我能想到的任何地方,但是搜索这个问题甚至很难)这是包含last-endpoint-created测试的文件。我可以添加任何其他可能有助于找到可能解决方案的文件!

'use strict';

/* globals describe, expect, it, beforeEach, afterEach */

var app = require('../..');
import request from 'supertest';

var newFinding;

describe('Finding API:', function() {
  describe('GET /api/findings', function() {
    var findings;

    beforeEach(function(done) {
      request(app)
        .get('/api/findings')
        .expect(200)
        .expect('Content-Type', /json/)
        .end((err, res) => {
          if(err) {
            return done(err);
          }
          findings = res.body;
          done();
        });
    });

    it('should respond with JSON array', function() {
      expect(findings).to.be.instanceOf(Array);
    });
  });

  describe('POST /api/findings', function() {
    beforeEach(function(done) {
      request(app)
        .post('/api/findings')
        .send({
          name: 'New Finding',
          info: 'This is the brand new finding!!!'
        })
        .expect(201)
        .expect('Content-Type', /json/)
        .end((err, res) => {
          if(err) {
            return done(err);
          }
          newFinding = res.body;
          done();
        });
    });

    it('should respond with the newly created finding', function() {
      expect(newFinding.name).to.equal('New Finding');
      expect(newFinding.info).to.equal('This is the brand new finding!!!');
    });
  });

  describe('GET /api/findings/:id', function() {
    var finding;

    beforeEach(function(done) {
      request(app)
        .get(`/api/findings/${newFinding._id}`)
        .expect(200)
        .expect('Content-Type', /json/)
        .end((err, res) => {
          if(err) {
            return done(err);
          }
          finding = res.body;
          done();
        });
    });

    afterEach(function() {
      finding = {};
    });

    it('should respond with the requested finding', function() {
      expect(finding.name).to.equal('New Finding');
      expect(finding.info).to.equal('This is the brand new finding!!!');
    });
  });

  describe('PUT /api/findings/:id', function() {
    var updatedFinding;

    beforeEach(function(done) {
      request(app)
        .put(`/api/findings/${newFinding._id}`)
        .send({
          name: 'Updated Finding',
          info: 'This is the updated finding!!!'
        })
        .expect(200)
        .expect('Content-Type', /json/)
        .end(function(err, res) {
          if(err) {
            return done(err);
          }
          updatedFinding = res.body[0];
          done();
        });
    });

    afterEach(function() {
      updatedFinding = {};
    });

    it('should respond with the updated finding', function() {
      expect(updatedFinding).to.equal(1);
    });

    it('should respond with the updated finding on a subsequent GET', function(done) {
      request(app)
        .get(`/api/findings/${newFinding._id}`)
        .expect(200)
        .expect('Content-Type', /json/)
        .end((err, res) => {
          if(err) {
            return done(err);
          }
          let finding = res.body;

          expect(finding.name).to.equal('Updated Finding');
          expect(finding.info).to.equal('This is the updated finding!!!');

          done();
        });
    });
  });
});
node.js mocha angular-fullstack
1个回答
0
投票

我找到了一个解决方法,我认为我已经找到了问题,但我不明白为什么现在发生,而不是之前。

在建立数据库连接之前正在执行一些测试,所以我已经添加了

before(function(done) {
    app.on('appStarted', function() {
      done();
    });
  });

到新的测试文件,现在它没有任何问题!

© www.soinside.com 2019 - 2024. All rights reserved.