gpt4 book ai didi

angularjs - 使用 Jasmine 进行端到端测试

转载 作者:行者123 更新时间:2023-12-02 22:56:27 24 4
gpt4 key购买 nike

我正在尝试对用 AngularJS 编写的应用程序执行一些端到端测试。目前,我在 e2e.tests.js 中设置了以下测试:

describe('MyApp', function() {
beforeEach(function() {
browser().navigateTo('../index.html');
});

it('should be true', function() {
expect(true).toBe(true);
});
});

奇怪的是,这个基本测试失败了。测试本身运行。然而结果是:

203ms   browser navigate to '../index.html'
5ms expect undefined toBe true
http://localhost:81/tests/e2e.tests.js:10:9
expected true but was undefined

考虑到“true”是硬编码的,我希望它能够通过这个测试。我不知道如何才能未定义 true。我错过了什么?

最佳答案

Angular 的端到端测试框架的特点是看起来像 Jasmine,但它又不是 Jasmine。 E2E测试代码实际上都是异步的,但它的编写方式很巧妙,使得调用看起来很正常。大多数调用都会创建异步任务和稍后测试的 Future 对象。 Future 对象有点像 promise ,但又有点不同。它有一个 value 属性,在准备就绪时设置该属性,然后调用 done 函数进入下一步。在 E2E 测试中,expect 函数采用 Future 对象,而不是值。您看到 undefined 是因为 expect 正在针对 future.value 进行测试,在本例中为 true.value ,这是未定义的。

尝试使用 available selectors that return futures 之一然后测试结果。像这样的事情:

expect(element("html").text()).toMatch("Our App");

Future 对象没有详细记录,但您应该能够像这样手动创建 Future:

var trueFuture = angular.scenario.Future(
"a true value", // name that is displayed in the results
function(callback) { // "behavior" function
callback(null, true); // supposed to call callback with (error, result)
});
expect(trueFuture).toEqual(true);

如果你查看 ng-scenario 源代码,你可以看到 place where the matcher tests future.value in the angular.scenario.matcher function .

关于angularjs - 使用 Jasmine 进行端到端测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19522931/

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