javascript – 测试Jasmine测试是否失败
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 测试Jasmine测试是否失败,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1831字,纯文字阅读大概需要3分钟。
内容图文
![javascript – 测试Jasmine测试是否失败](/upload/InfoBanner/zyjiaocheng/805/ed0f78cdeac64ff3aee38621bc51bdf7.jpg)
我正在尝试为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()
})
})
内容总结
以上是互联网集市为您收集整理的javascript – 测试Jasmine测试是否失败全部内容,希望文章能够帮你解决javascript – 测试Jasmine测试是否失败所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。