摩卡失败,许多测试都在运行

时间:2017-01-13 16:53:25

标签: node.js mocha

我有一些类似的测试,都在端口4000中运行,但有些测试失败了。当我把测试运行在端口不同的时候,我取得了成功。我不明白为什么。

有人知道原因吗?

describe('Material Controller', () => {

    // create a request object with cookied enabled
    var request = require('request').defaults({ jar: true });
    var server: Startup;
    var baseUrl = 'http://localhost:4000';
    var session: Session;
    var materialName = 'product material';
    var materialId;
    var form = {
        name: 'owner',
        imageUrl: 'image url'
    }

    before((done) => {

        init("4000", async (err, result) => {
            if (err) return done(err);

            server = result;

            session = server.sessionFactory.createSession();

            var materialToUpdate = new Material('new name', 'new image url');
            await session.save(materialToUpdate);
            materialId = materialToUpdate.id;

            // flush the session to write changes to the database but keep the session open to remove the user in the 'after' hook
            session.flush((err) => {
                if (err) return done(err);

                // since cookies are enabled the authentication will work for all tests that follow
                // authenticate(done);
                done();
            });

        });
    });

    after((done) => {


        // delete the user that was created by the service if one exists
        session.query(Material).findOne({ _name: materialName }, (err, storeSaved) => {
            if (err) return done(err);

            if (!storeSaved) {
                return done();
            }

            // delete the user created by the service
            session.remove(storeSaved);

            // flush changes to the database and close the session
            session.close((err) => {
                if (err) return done(err);

                // shutdown the http server
                server.close(done);
            });
        });
    });

    it('should return success when save a material', (done) => {

        request.post(
            {
                url: baseUrl + '/v1/material',
                form: form
            },
            (error, resp, body) => {

                var bodyJson = JSON.parse(body);
                assert.isTrue(bodyJson.success);
                done();
            });
    });

    it('should return success when update a material by id', (done) => {

        var form = {
            id: materialId,
            name: 'new name',
            imageUrl: 'new image'
        };

        request.post(
            {
                url: baseUrl + '/v1/material',
                form: form
            },
            (error, resp, body) => {

                var bodyJson = JSON.parse(body);
                assert.isTrue(bodyJson.success);
                assert.equal(materialId, bodyJson.result);
                done();
            });
    });
});

0 个答案:

没有答案