使用httpbackend测试ngResource - 可能出现未处理的拒绝错误

11 浏览
0 Comments

使用httpbackend测试ngResource - 可能出现未处理的拒绝错误

问题:

我正在尝试使用Karma,Mocha,Chai和PhantomJS来测试以下控制器

IndexCtrl.js(简化)

app.controller('IndexCtrl',['$scope','$resource',function($scope,$resource){

var methods = {

get:{

method:'GET'

}

};

var Users = $resource('/api/v1/search/:search',{},methods);

$scope.testResource = function(){

Users.get({search:'TEST'},function(result){

$scope.someTestValue = true;

},function(){

console.log("hello world");

});

};

}]);

index.controller.test.js(再次简化,我还有其他非HTTP测试)

describe('Index',function(){

var ctrl,$httpBackend;

beforeEach(module('myApp'));

beforeEach(inject(function($rootScope,$controller,$injector){

$httpBackend = $injector.get('$httpBackend');

$scope = $rootScope.$new();

ctrl = $controller('IndexCtrl',{

$scope:$scope

});

}));

describe('Search',function(){

it('Changing the override should trigger the watcher',function(){

$httpBackend.expectGET('/.*/',undefined).respond(200,{});

$scope.testResource();

$httpBackend.flush();

expect($scope.someTestValue).to.equal(true);

});

});

});

当我运行测试时,我得到以下错误。如果我注释掉flush行,它不会抛出此错误,但它无法正常工作。

PhantomJS 2.1.1(Mac OS X 0.0.0)索引搜索更改覆盖应该触发观察程序失败

抛出字符串“可能未处理的拒绝:{“行”:1421,“sourceURL”:“node_modules / angular-mocks /angular-mocks.js”,“stack”:“$ httpBackend @ node_modules / angular-mocks /angular-mocks.js:1421:90 \ nsendReq @ webroot / js / angular.compiled.min.js:4:16511 \ nserverRequest @ webroot / js / angular.compiled.min.js:4:12080 \ nprocessQueue @ webroot / js / angular.compiled.min.js:5:14235 \ nwebroot / js / angular.compiled.min.js:5:15007 \ n $ eval @ webroot / js / angular.compiled.min.js:5:46097 \ n $ digest @ webroot / js / angular.compiled.min.js:5:43824 \ nflish @ node_modules / angular-mocks /angular-mocks.js:1813:45 \ nweb-test / index.controller.test.js:18:22 \ ncallFn @ node_modules / mocha / mocha.js:4447:25 \ nrun @ node_modules / mocha / mocha.js:4439:13 \ nrunTest @ node_modules / mocha / mocha.js:4936:13 \ nnode_modules / mocha / mocha.js:5042:19 \ nnext @ node_modules / mocha / mocha.js:4853:16 \ nnode_modules / mocha / mocha.js:4863:11 \ nnext @ node_modules / mocha / mocha.js:4787:16 \ nnode_modules / mocha / mocha.js:4831:9 \ ntimeslice @ node_modules / mocha / mocha.js:82:27“}”,抛出错误:)

package.json

{

"name":“myApp”,

"version":“1.0.0”,

"description":“”,

"main":“index.js”,

"scripts":{

“test”:“mocha”,

“web-test”:“node ./node_modules/karma/bin/karma start karma.conf.js”

},

"author":“”,

"license":“ISC”,

"dependencies":{

“angular”:“1.5.9”,

“angular-animate”:“1.5.9”,

“angular-lazy-image”:“^0.3.2”,

“angular-resource”:“1.5.9”,

“angular-sanitize”:“1.5.9”,

“angular-typewriter”:“0.0.15”,

“angular-ui-router”:“^0.3.2”,

“body-parser”:“^1.15.2”,

“cookie-parser”:“^1.4.3”,

“elasticsearch”:“^12.1.3”,

“express”:“^4.14.0”,

“grunt”:“*”,

“grunt-contrib-sass”:“*”,

“grunt-contrib-uglify”:“*”,

“grunt-contrib-watch”:“*”,

“grunt-html2js”:“*”,

“grunt-notify”:“*”,

“ldapjs”:“^1.0.1”,

“request”:“^2.79.0”

},

"devDependencies":{

“angular-mocks”:“^1.6.0”,

“chai”:“^3.5.0”,

“karma”:“^1.3.0”,

“karma-chai”:“^0.1.0”,

“karma-chrome-launcher”:“^2.0.0”,

“karma-cli”:“^1.0.1”,

“karma-jasmine”:“^1.1.0”,

“karma-mocha”:“^1.3.0”,

“karma-phantomjs-launcher”:“^1.0.2”,

“karma-requirejs”:“^1.1.0”,

“mocha”:“^3.2.0”,

“requirejs”:“^2.3.2”

}

}

Mods注意:

这与“Angular 1.6.0:”可能未处理的拒绝“错误不同,其中错误不是由flush触发的,解决方案是回滚到angularjs 1.5.9。我已经尝试回滚到angular 1.5.9,但没有效果。因此,亲爱的上帝,如果我再次得到另一个盲目的“标记为重复”...

0
0 Comments

在使用Angular版本1.5.9到1.6.0时,可能会出现未处理拒绝的错误。为了解决这个问题,可以在应用程序的配置中添加以下代码:

.config(['$qProvider', function ($qProvider) {
    $qProvider.errorOnUnhandledRejections(false);
}])

这段代码的作用是禁用未处理拒绝的错误提示。通过将`errorOnUnhandledRejections`设置为`false`,可以防止出现未处理拒绝的错误。

这个问题的根本原因是在Angular 1.5.9到1.6.0版本中存在某种与未处理拒绝相关的bug。当使用`ngResource`与`httpbackend`进行测试时,可能会出现未处理拒绝的错误。为了解决这个问题,可以通过禁用未处理拒绝的错误提示来避免错误的出现。

以上就是解决使用`ngResource`与`httpbackend`进行测试时可能出现的"Testing ngResource with httpbackend - Possibly unhandled rejection error"问题的原因和解决方法。通过在应用程序的配置中添加一段代码,可以禁用未处理拒绝的错误提示,从而避免出现该错误。

0