beforeEach 未在 angularjs/ requirejs 场景中通过 Karma/ Jasmine 调用
beforeEach not called via Karma/ Jasmine in an angularjs/ requirejs scenario
运行 我的单元测试在本地运行良好,但在构建服务器上发生了最愚蠢的事情:有时它们通过了,有时却没有。主要代码:
define(['angmock', 'someMore'], function () {
describe('myController', function () {
beforeEach(function () {
console.log("Entry - Initializing modules...");
module('module1');
module('module2');
console.log("Exit - Initializing modules...");
});
var scope, controller;
beforeEach(inject(function ($rootScope, $controller) {
console.log("Entry - Creating scope...");
scope = $rootScope.$new();
controller = $controller('myController', {
$scope: scope
});
console.log("Exit - Creating scope...");
}));
it('should test something', function () {
console.log("Entry - Test A");
scope.myImportantAssignment = variableName;
...
console.log("Exit - Test A");
});
...
在构建服务器日志中,我有时可以读到:
TypeError: undefined is not an object (evaluating
'scope.myImportantAssignment = variableName')
在控制台上我可以阅读:
- 日志:'Entry - Initializing modules...'
- 日志:'Exit - Initializing modules...'
- 日志:'Entry - Test A'
这可能表明根本没有调用第二个 beforeEach,因此范围没有初始化。但为什么?在我看来,处理应该像这样:
- 在每个 1
之前
- 在每个 2
之前
- 它
但好像不是这样。异步可能存在一些问题吗?非常感谢任何小提示,因为如果测试有时失败,我根本无法使用它们...
没有可以解释问题的竞争条件,块是同步的并且总是 运行 按顺序。如果应用程序静默无法引导并在 inject
.
上抛出,则可能会发生这种情况
控制台中应该有错误,但 PhantomJS 以吞噬错误而闻名,将 Karma 启动器更改为 Chrome 可能会改善错误输出。
将 inject
从 beforeEach
移动到 it
也可以帮助定位问题,它使规范失败不是因为预期失败,而是因为使预期失败的错误失败。
在单元测试中,应用程序单元被测试,其他一切都应该被模拟,具有额外的依赖性(Angular 包装器 Kendo UI)向规范添加了更多移动部分。
运行 我的单元测试在本地运行良好,但在构建服务器上发生了最愚蠢的事情:有时它们通过了,有时却没有。主要代码:
define(['angmock', 'someMore'], function () {
describe('myController', function () {
beforeEach(function () {
console.log("Entry - Initializing modules...");
module('module1');
module('module2');
console.log("Exit - Initializing modules...");
});
var scope, controller;
beforeEach(inject(function ($rootScope, $controller) {
console.log("Entry - Creating scope...");
scope = $rootScope.$new();
controller = $controller('myController', {
$scope: scope
});
console.log("Exit - Creating scope...");
}));
it('should test something', function () {
console.log("Entry - Test A");
scope.myImportantAssignment = variableName;
...
console.log("Exit - Test A");
});
...
在构建服务器日志中,我有时可以读到:
TypeError: undefined is not an object (evaluating 'scope.myImportantAssignment = variableName')
在控制台上我可以阅读:
- 日志:'Entry - Initializing modules...'
- 日志:'Exit - Initializing modules...'
- 日志:'Entry - Test A'
这可能表明根本没有调用第二个 beforeEach,因此范围没有初始化。但为什么?在我看来,处理应该像这样:
- 在每个 1 之前
- 在每个 2 之前
- 它
但好像不是这样。异步可能存在一些问题吗?非常感谢任何小提示,因为如果测试有时失败,我根本无法使用它们...
没有可以解释问题的竞争条件,块是同步的并且总是 运行 按顺序。如果应用程序静默无法引导并在 inject
.
控制台中应该有错误,但 PhantomJS 以吞噬错误而闻名,将 Karma 启动器更改为 Chrome 可能会改善错误输出。
将 inject
从 beforeEach
移动到 it
也可以帮助定位问题,它使规范失败不是因为预期失败,而是因为使预期失败的错误失败。
在单元测试中,应用程序单元被测试,其他一切都应该被模拟,具有额外的依赖性(Angular 包装器 Kendo UI)向规范添加了更多移动部分。