无法读取属性'spyOn'of null-模拟angularJS单元测试中的承诺
Cannot read property 'spyOn' of null - Mocking a promise in angularJS unit test
我正在尝试模拟一个函数,该函数使用spyOn返回promise,代码如下:
beforeEach(inject(function($controller, $rootScope, $q) {
scope = $rootScope.$new();
q = $q;
ctrl = $controller('BillingCtrl', {
$scope: scope,
$q: q
});
}));
it('should have overlay off when cancel modal is shown',
function() {
var deferred = q.defer();
deferred.resolve();
spyOn(scope, 'confirmModal').andReturn(deferred.promise);
scope.cancelSubscription(""); //scope.confirmModal is called within here.
expect(scope.overlayOn).to.equal(true);
});
这引发了以下错误:
Chrome 37.0.2062 (Mac OS X 10.9.2) Unit: BillingController "before each" hook: workFn FAILED
TypeError: Cannot read property 'spyOn' of null
scope.confirmModal
在这种情况下肯定存在,因为我可以对其进行console.log。
任何建议都将不胜感激!
我想明白了。
Mocha和Jasmine有冲突(我都用了)。
当我移除Mocha时,spyOn功能运行良好。
相关文章:
- AngularJS指令单元测试中未定义的函数
- AngularJS:指令限制:“E”阻止在Jasmine单元测试中调用元素click()事件
- 如何在angularjs单元测试中触发mousemove事件
- angularjs单元测试何时使用$rootScope$new()
- AngularJS列出了所有单元测试方法
- 使用karma浏览器对AngularJS应用程序进行单元测试
- AngularJS单元测试指令's属性值
- 如何在AngularJS中承诺控制器后进行单元测试
- AngularJs的单元测试失败是因为Karma没有'无法识别$scope$控制器中的侦听器
- 单元测试AngularJS控制器,同时遵循最佳实践
- 不确定 AngularJS 单元测试中的错误
- 当使用 Jasmine 对我的 angularJS 应用程序进行单元测试时,$interval是未定义的
- 单元测试:AngularJS + Karma + Jasmine,加载本地JSON
- 单元测试angularjs$q.all-承诺永远不会完成
- 错误:单元测试AngularJs服务时,没有待处理的flush请求
- Jasmine单元测试AngularJS工厂的两个依赖项($http和另一个返回promise的工厂)
- 单元测试AngularJS模块控制器
- 单元测试AngularJS服务
- 在单元测试AngularJS服务时注入依赖服务
- 单元测试angularjs指令监听事件