简体   繁体   中英

Testing if a Jasmine Test Fails

I'm trying to write a plugin for Jasmine that allows you to return a promise from a spec and will pass or fail that spec depending on whether or not the promise is fulfilled or rejected.

Of course, I want to write tests to make sure that my plugin works correctly, and to be thorough, I need to make sure that tests fail when the promise is rejected... so how do I make a test pass when I need to make sure that a test "successfully fails"?

After a conversation with the developers who work on Jasmine, we've come up with this:

var FAILED = 'failed'
var PASSED = 'passed'

describe('My Test Suite', function () {
    var env

    beforeEach(function () {
        // Create a secondary Jasmine environment to run your sub-specs in
        env = new jasmine.Env()
    })

    it('should work synchronously', function () {
        var spec

        // use the methods on `env` rather than the global ones for sub-specs
        // (describe, it, expect, beforeEach, etc)
        env.describe('faux suite', function () {
            spec = env.it('faux test', function (done) {
                env.expect(true).toBe(true)
            })
        })

        // this will fire off the specs in the secondary environment
        env.execute()

        // put your expectations here if the sub-spec is synchronous
        // `spec.result` has the status information we need
        expect(spec.result.status).toBe(FAILED)
    })

    // don't forget the `done` argument for asynchronous specs 
    it('should work asynchronously', function (done) {
        var spec

        // use the methods on `env` rather than the global ones.
        env.describe('faux suite', function () {
            // `it` returns a spec object that we can use later
            spec = env.it('faux test', function (done) {
                Promise.reject("FAIL").then(done)
            })
        })

        // this allows us to run code after we know the spec has finished
        env.addReporter({jasmineDone: function() {
            // put your expectations in here if the sub-spec is asynchronous
            // `spec.result` has the status information we need
            expect(spec.result.status).toBe(FAILED)
            // this is how Jasmine knows you've completed something asynchronous
            // you need to add it as an argument to the main `it` call above
            done()
        }})

        // this will fire off the specs in the secondary environment
        env.execute()
    })
})

Going off Joe's answer, I moved the fake test context into a single function. Since the code under test is making use of jasmine expectations, I load the inner Env into jasmine.currentEnv_ and call it explicitly with jasmine.currentEnv_.expect() . Note that currentEnv_ is an internal variable set by jasmine itself, so I can't guarantee that this won't be broken in a future jasmine version.

function internalTest(testFunc) {
    var outerEnvironment = jasmine.currentEnv_;
    var env = new jasmine.Env();
    jasmine.currentEnv_ = env;
    var spec;
    env.describe("fake suite", function () {
        spec = env.it("fake test", function () {
            func();
        });
    });

    env.execute();

    jasmine.currentEnv_ = outerEnvironment;

    return spec.result;
}

Then each test looks like

it("does something", function () {
    //Arrange

    //Act
    var result = internalTest(function () {
        //Perform action
    });

    //Assert
    expect(result.status).toBe("failed"); //Or "success"
    expect(result.failedExpectations.length).toBe(1);
    expect(result.failedExpectations[0].message).toBe("My expected error message");
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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