最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to run qunit assertions on resolving a Promise - Stack Overflow

programmeradmin4浏览0评论

I am writing a test for a function that returns a promise, and am not able to run assertions on resolving or rejecting the promise. I cannot use ES6 on this project so I am using the rsvp.js library which claims to implement the Promises/A+ specification. I am using the latest qunit version 1.17.1

The function under test:

var loadGroups = function(data) {
     return new RSVP.Promise(function(resolve, reject) {
        var groups = [];
        // ... some code to populate the groups array
        if(groups.length > 0) {
            resolve(groups);
        } else {
            reject("No groups were loaded");
        }
    });
};

The success test:

test('success test', function(assert) {
    assert.expect(1);

    var promise = loadGroups(testData);

    promise.then(function(groups) {
        assert.deepEquals(groups.length, 1);
    });

    return promise;
});

This fails with "Expected 1 assertions, but 0 were run"


The failure test:

test('failure test', function(assert) {
    assert.expect(1);

    var promise = loadGroups([]);

    promise.then(null, function(message) {
        assert.equals(message, "No groups were loaded");
    });

    return promise;
});

This fails with "Promise rejected during get promise when loading empty groups: No groups were loaded"

I am writing a test for a function that returns a promise, and am not able to run assertions on resolving or rejecting the promise. I cannot use ES6 on this project so I am using the rsvp.js library which claims to implement the Promises/A+ specification. I am using the latest qunit version 1.17.1

The function under test:

var loadGroups = function(data) {
     return new RSVP.Promise(function(resolve, reject) {
        var groups = [];
        // ... some code to populate the groups array
        if(groups.length > 0) {
            resolve(groups);
        } else {
            reject("No groups were loaded");
        }
    });
};

The success test:

test('success test', function(assert) {
    assert.expect(1);

    var promise = loadGroups(testData);

    promise.then(function(groups) {
        assert.deepEquals(groups.length, 1);
    });

    return promise;
});

This fails with "Expected 1 assertions, but 0 were run"


The failure test:

test('failure test', function(assert) {
    assert.expect(1);

    var promise = loadGroups([]);

    promise.then(null, function(message) {
        assert.equals(message, "No groups were loaded");
    });

    return promise;
});

This fails with "Promise rejected during get promise when loading empty groups: No groups were loaded"

Share Improve this question edited Feb 24, 2015 at 12:17 carbontax asked Feb 24, 2015 at 1:46 carbontaxcarbontax 2,18424 silver badges38 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 16

promises work by chaining. Promises are immutable wrappers over values.

When you do:

promise.then(function(e){
   // do something
});

You are not changing promise you are creating a new promise instead. Instead, you need to chain the promise:

test('success test', function(assert) {
    assert.expect(1);

    var promise = loadGroups(testData);

    // return the `then` and not the original promise.
    return promise.then(function(groups) {
        assert.deepEquals(groups.length, 1);
    });

});

Starting version 1.16.0 was added assert.async()

So you can now run:


test('success test', function(assert) {
    const done = assert.async();
    assert.expect(1);

    var promise = loadGroups(testData);

    promise.then(function(groups) {
        assert.deepEquals(groups.length, 1);
        done(); // << inform that you have plete checking
    }).catch((err) => {/*...*/ done();});
});

发布评论

评论列表(0)

  1. 暂无评论