mercredi 22 février 2017

mocha - Retrieve result of promise test

I want to get the result of promise in before

describe('unsubscribe', function() {
        var arn;
        this.timeout(10000);
        before(function(done) {
            sns.subscribe('param1', 'param2').then(
                (result) => {
                    arn = result;
                    done();
                },
                (error) => assert.fail(error));
        });
        it('("param") => promise returns object', function() {
            const result = sns.unsubscribe(arn);
            expect(result).to.eventually.be.an('object');
        });
    });

Similarly, in after I want to get result of promise in test

describe('subscribe', function() {
        var arn;
        this.timeout(10000);
        it('("param1","param2") => promise returns string', function() {
            sns.subscribe('param1', 'param2').then(
                (result) => {
                    arn = result;
                    expect(result).to.be.a('string');
                },
                (error) => {
                    assert.fail(error);
                    done();
                });
        });
        after(function(done) {
            sns.unsubscribe(arn).then(
                (result) => done());
        });
    });

Is this code properly written? Is there any better practice? What is the recommended way to do so?

Aucun commentaire:

Enregistrer un commentaire