测试Jasmine测试是否失败
Testing if a Jasmine Test Fails
我正在尝试为Jasmine编写一个插件,允许您从规范返回承诺,并将通过或失败该规范取决于承诺是否被履行或拒绝。
当然,我想写测试来确保我的插件正常工作,为了彻底,我需要确保当承诺被拒绝时测试失败…那么,当我需要确保测试"成功失败"时,我如何使测试通过呢?
在与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");
});
相关文章:
- ChaiJS:无法导致测试失败
- 当我的单元测试失败时,回调被调用了两次
- 预期[false]为false量角器e2e测试失败
- 为什么在我的单元测试中instanceOf的测试失败
- yeoman generator聚合物的web组件测试失败
- 当Firefox尝试登录时,Selenium测试失败
- 量角器中的AngularJS E2E测试失败
- 用摩卡测试失败的承诺'内置的promise支持
- 这个单元测试失败的原因是什么
- 当分离到不同的文件中时,测试失败
- travis测试失败,通过本地测试
- 字符串测试失败
- Javascript单元测试依赖关系-当依赖对象发生变化时,如何使测试失败
- 如何在捕获Promise时使Node单元测试失败
- AngularJs的单元测试失败是因为Karma没有'无法识别$scope$控制器中的侦听器
- 有没有办法在node.js中的异常或测试失败时启动调试器
- Capybara JS 测试失败了 page.all
- 现代化测试失败后仍应用函数
- 角度咆哮 V2 - 单元测试失败
- 为什么这个 QUnit 测试失败