gpt4 book ai didi

javascript - 测试 Jasmine 测试是否失败

转载 作者:搜寻专家 更新时间:2023-11-01 05:10:38 25 4
gpt4 key购买 nike

我正在尝试为 Jasmine 编写插件这允许您从规范中返回一个 promise ,并根据该 promise 是被履行还是被拒绝来通过或失败该规范。

当然,我想编写测试以确保我的插件正常工作,并且为了彻底,我需要确保在 promise 被拒绝时测试失败......那么当我如何让测试通过时我需要确保测试“成功失败”?

最佳答案

在与 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 测试是否失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32617196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com