gpt4 book ai didi

unit-testing - 使用 Mocha 和 Chai 测试 Angular

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

我想用 Yeoman 测试我的 angular 应用程序,它使用带有 Phantom 和 Chai 的 Mocha 进行断言。
但是当我运行任何示例测试用例时,测试用例无法正常运行,它显示 PhantomJs 由于缺少 Mocha run() 调用而超时。非 Angular 用例在测试用例中工作正常。

<!doctype html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Mocha Spec Runner</title>
<link rel="stylesheet" href="lib/mocha/mocha.css">
</head>
<body>
<div id="mocha"></div>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js"></script>
<script src="lib/mocha/mocha.js"></script>
<script>mocha.setup('bdd')</script>
<script src="lib/chai.js"></script>
<script>
expect = chai.expect;
assert = chai.assert;
</script>
<script>
function addSum(num1, num2) {
return num1 + num2;
}
</script>
<script>
(function() {
describe('Give it some context', function() {
it('should simulate promise', inject(function ($q, $rootScope) {
assert.notStrictEqual(3, '3', 'no coercion for strict equality');
/* var deferred = $q.defer();
var promise = deferred.promise;
var resolvedValue;

promise.then(function(value) { resolvedValue = value; });
expect(resolvedValue).to.be.undefined;

// Simulate resolving of promise
deferred.resolve(123);
// Note that the 'then' function does not get called synchronously.
// This is because we want the promise API to always be async, whether or not
// it got called synchronously or asynchronously.
expect(resolvedValue).to.be.undefined

// Propagate promise resolution to 'then' functions using $apply().
$rootScope.$apply();
expect(resolvedValue).to.equal(123);*/
}));
});
})();
</script>



<!-- trigger the mocha runner -->
<script src="runner/mocha.js"></script>

</body>
</html>

最佳答案

您是否尝试过使用 Protractor ?它是专门为测试端到端 angularjs 应用程序而开发的(由 angularjs 团队开发)。 https://github.com/angular/protractor

它有自己的运行器,您可以安装它:

npm install protractor -g

然后运行程序执行:
protractor /configfile.cfg

无需 HTML 页面即可运行测试。

配置文件非常简单(您可以在源代码中看到选项)。

有了这个,你的规范定义为:
// myTest.js
describe('angularjs homepage', function() {
it('should greet the named user', function() {
browser.get('http://www.angularjs.org');
element(by.model('yourName')).sendKeys('Julie');

var greeting = element(by.binding('yourName'));

expect(greeting.getText()).toEqual('Hello Julie!');
});
});

关于unit-testing - 使用 Mocha 和 Chai 测试 Angular,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16080559/

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