测试 Jasmine 测试是否失败
Testing if a Jasmine Test Fails
我正在尝试为 Jasmine 编写一个插件,允许您 return 规范中的承诺,并根据承诺是否实现或是否通过该规范来决定是否通过该规范拒绝了。
当然,我想编写测试以确保我的插件正常工作,并且为了彻底,我需要确保在 promise 被拒绝时测试失败...那么我该如何进行测试当我需要确保测试通过时 "successfully fails"?
在与 Jasmine 的开发人员交谈后,我们得出以下结论:
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()
})
})
根据 Joe 的回答,我将伪造的测试上下文移到了一个函数中。由于被测代码正在使用 jasmine 期望,我将内部 Env
加载到 jasmine.currentEnv_
并使用 jasmine.currentEnv_.expect()
显式调用它。注意 currentEnv_
是 jasmine 自己设置的内部变量,所以我不能保证在未来的 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;
}
然后每个测试看起来像
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");
});
我正在尝试为 Jasmine 编写一个插件,允许您 return 规范中的承诺,并根据承诺是否实现或是否通过该规范来决定是否通过该规范拒绝了。
当然,我想编写测试以确保我的插件正常工作,并且为了彻底,我需要确保在 promise 被拒绝时测试失败...那么我该如何进行测试当我需要确保测试通过时 "successfully fails"?
在与 Jasmine 的开发人员交谈后,我们得出以下结论:
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()
})
})
根据 Joe 的回答,我将伪造的测试上下文移到了一个函数中。由于被测代码正在使用 jasmine 期望,我将内部 Env
加载到 jasmine.currentEnv_
并使用 jasmine.currentEnv_.expect()
显式调用它。注意 currentEnv_
是 jasmine 自己设置的内部变量,所以我不能保证在未来的 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;
}
然后每个测试看起来像
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");
});