Going away from Joe's answer, I moved the fake test context to one function. Since the code under test uses jasmine expectations, I load the internal one Envin jasmine.currentEnv_and call it explicitly with jasmine.currentEnv_.expect(). Please note that this currentEnv_is an internal variable set by jasmine itself, so I can not guarantee that this will not be violated in a future version of jasmine.
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 () {
var result = internalTest(function () {
});
expect(result.status).toBe("failed");
expect(result.failedExpectations.length).toBe(1);
expect(result.failedExpectations[0].message).toBe("My expected error message");
});
source
share