gpt4 book ai didi

jasmine - 异步调用 beforeAll

转载 作者:行者123 更新时间:2023-12-01 03:38:25 26 4
gpt4 key购买 nike

这是同一测试的 2 个样本。唯一的区别是第一个使用了 beforeAll 中的 promise 。块为变量赋值,而第二个块直接赋值。

我提出了类似的问题 Running spec after promise has been resolved其中一条评论指向此问题 https://github.com/jasmine/jasmine/issues/412这表示 Jasmine 不支持此功能。有人想出任何解决方法吗?

这失败了 TypeError: Cannot read property 'forEach' of undefined

describe('Async car test', function () {

var cars;

beforeAll(function (done) {
// getCars() is a promise which resolves to ['audi', 'bmw']
getCars().then(function (data) {
cars = data;
console.log(cars) // ['audi', 'bmw']
done();
});
});

cars.forEach(function (car) {
it('car ' + car, function () {
expect(car).toBe(car);
});
});
});

这工作正常
describe('Car test', function () {

var cars = ['audi', 'bmw'];

cars.forEach(function (car) {
it('car ' + car, function () {
expect(car).toBe(car);
});
});
});

最佳答案

这不是 Jasmine 的问题,而是您的代码的问题。
beforeAll 不会阻塞语句下面的后续代码。它阻塞了定义在其中的代码('should ...', (done)=>{...});

it('should have cars', (done) => {  
cars.forEach(function (car) {
expect(car).toBe(car);
});
});

关于jasmine - 异步调用 beforeAll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32768333/

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