Karma Jasmine 测试从控制器调用的 $resource
Karma Jasmine testing $resource called from controller
为了测试某些 API 调用,我无法设法让 Karma 正常工作。
这是测试文件:
describe('Requests controller test', function() {
beforeEach(module('balrogApp.requests'));
var ctrl, scope;
var requestData = [
{id: 1, project: {id: 1, title: 'Project 1'}, description: 'Some description'},
{id: 2, project: {id: 2, title: 'Project 2'}, description: 'Another description'}
];
beforeEach(inject(function($rootScope, $controller, _$httpBackend_) {
$httpBackend = _$httpBackend_;
$httpBackend.expectGET('/users').respond(requestData);
$httpBackend.expectGET('/requests').respond(requestData);
$httpBackend.expectGET('/projects').respond(requestData);
$httpBackend.expectGET('/requestcomments').respond(requestData);
$httpBackend.expectGET('/costestimations').respond(requestData);
$httpBackend.expectGET('/regions').respond(requestData);
scope = $rootScope.$new();
ctrl = $controller('requestsController', {$scope: scope});
}));
afterEach(function() {
scope.$destroy();
});
it('should fill properties from result from xhr requests', function() {
var unresolvedResponse = [];
expect(ctrl.usersList).toEqual(unresolvedResponse);
expect(ctrl.requestsList).toEqual(unresolvedResponse);
expect(ctrl.projectsList).toEqual(unresolvedResponse);
expect(ctrl.requestsCommentsList).toEqual(unresolvedResponse);
expect(ctrl.costEstimationsList).toEqual(unresolvedResponse);
expect(ctrl.regionsList).toEqual(unresolvedResponse);
$httpBackend.flush();
expect(ctrl.usersList).toEqual(requestData);
expect(ctrl.requestsList).toEqual(requestData);
expect(ctrl.projectsList).toEqual(requestData);
expect(ctrl.requestsCommentsList).toEqual(requestData);
expect(ctrl.costEstimationsList).toEqual(requestData);
expect(ctrl.regionsList).toEqual(requestData);
});
});
我也尝试过使用 toBeUndefined()
而不是 toEqual(unresolvedResponse)
但它并没有改变任何事情。
这是定义 $resource 的文件:
angular.module('balrogApp.services', ['balrogApp.config', 'ngResource'])
.factory('Requests', ['$resource', 'balrogConfig', function($resource, balrogConfig) {
return $resource(balrogConfig.backend + '/requests/:id', {id: '@id'});
}])
.factory('Projects', ['$resource', 'balrogConfig', function($resource, balrogConfig) {
return $resource(balrogConfig.backend + '/projects/:id', {id: '@id'}, {'update': { method:'PUT' }});
}])
/* Other factories are there */
.factory('CostEstimations', ['$resource', 'balrogConfig', function($resource, balrogConfig) {
return $resource(balrogConfig.backend + '/costestimations/:id', {id: '@id'});
}]);
最后,我正在测试的控制器文件的一部分:
angular.module('balrogApp.requests', [
/* Dependancies */
])
.controller('requestsController', function(Requests, Users, Projects, RequestsComments, CostEstimations,
Regions, growl, $route, $rootScope, $scope, $location) {
/* ... */
this.usersList = Users.query();
this.requestsList = Requests.query();
this.projectsList = Projects.query();
this.requestsCommentsList = RequestsComments.query();
this.costEstimationsList = CostEstimations.query();
this.regionsList = Regions.query();
});
到目前为止我遇到了这个错误:
Expected [ $promise: Promise({ $$state: Object({ status: 0 }) }), $resolved: false ] to equal [ ].
我尝试将 unresolvedResponse
设置为此值(有或没有正确的语法)但它没有解决任何问题。
我自己试过了,你如何做到这一点,正在改变你对
的期望
expect(ctrl.usersList).toEqual(unresolvedResponse);
至
expect(ctrl.usersList.$resolved).toBeFalsy();
执行此操作将验证请求已发送,承诺已构建但尚未有服务器响应。
希望对您有所帮助。
为了测试某些 API 调用,我无法设法让 Karma 正常工作。
这是测试文件:
describe('Requests controller test', function() {
beforeEach(module('balrogApp.requests'));
var ctrl, scope;
var requestData = [
{id: 1, project: {id: 1, title: 'Project 1'}, description: 'Some description'},
{id: 2, project: {id: 2, title: 'Project 2'}, description: 'Another description'}
];
beforeEach(inject(function($rootScope, $controller, _$httpBackend_) {
$httpBackend = _$httpBackend_;
$httpBackend.expectGET('/users').respond(requestData);
$httpBackend.expectGET('/requests').respond(requestData);
$httpBackend.expectGET('/projects').respond(requestData);
$httpBackend.expectGET('/requestcomments').respond(requestData);
$httpBackend.expectGET('/costestimations').respond(requestData);
$httpBackend.expectGET('/regions').respond(requestData);
scope = $rootScope.$new();
ctrl = $controller('requestsController', {$scope: scope});
}));
afterEach(function() {
scope.$destroy();
});
it('should fill properties from result from xhr requests', function() {
var unresolvedResponse = [];
expect(ctrl.usersList).toEqual(unresolvedResponse);
expect(ctrl.requestsList).toEqual(unresolvedResponse);
expect(ctrl.projectsList).toEqual(unresolvedResponse);
expect(ctrl.requestsCommentsList).toEqual(unresolvedResponse);
expect(ctrl.costEstimationsList).toEqual(unresolvedResponse);
expect(ctrl.regionsList).toEqual(unresolvedResponse);
$httpBackend.flush();
expect(ctrl.usersList).toEqual(requestData);
expect(ctrl.requestsList).toEqual(requestData);
expect(ctrl.projectsList).toEqual(requestData);
expect(ctrl.requestsCommentsList).toEqual(requestData);
expect(ctrl.costEstimationsList).toEqual(requestData);
expect(ctrl.regionsList).toEqual(requestData);
});
});
我也尝试过使用 toBeUndefined()
而不是 toEqual(unresolvedResponse)
但它并没有改变任何事情。
这是定义 $resource 的文件:
angular.module('balrogApp.services', ['balrogApp.config', 'ngResource'])
.factory('Requests', ['$resource', 'balrogConfig', function($resource, balrogConfig) {
return $resource(balrogConfig.backend + '/requests/:id', {id: '@id'});
}])
.factory('Projects', ['$resource', 'balrogConfig', function($resource, balrogConfig) {
return $resource(balrogConfig.backend + '/projects/:id', {id: '@id'}, {'update': { method:'PUT' }});
}])
/* Other factories are there */
.factory('CostEstimations', ['$resource', 'balrogConfig', function($resource, balrogConfig) {
return $resource(balrogConfig.backend + '/costestimations/:id', {id: '@id'});
}]);
最后,我正在测试的控制器文件的一部分:
angular.module('balrogApp.requests', [
/* Dependancies */
])
.controller('requestsController', function(Requests, Users, Projects, RequestsComments, CostEstimations,
Regions, growl, $route, $rootScope, $scope, $location) {
/* ... */
this.usersList = Users.query();
this.requestsList = Requests.query();
this.projectsList = Projects.query();
this.requestsCommentsList = RequestsComments.query();
this.costEstimationsList = CostEstimations.query();
this.regionsList = Regions.query();
});
到目前为止我遇到了这个错误:
Expected [ $promise: Promise({ $$state: Object({ status: 0 }) }), $resolved: false ] to equal [ ].
我尝试将 unresolvedResponse
设置为此值(有或没有正确的语法)但它没有解决任何问题。
我自己试过了,你如何做到这一点,正在改变你对
的期望expect(ctrl.usersList).toEqual(unresolvedResponse);
至
expect(ctrl.usersList.$resolved).toBeFalsy();
执行此操作将验证请求已发送,承诺已构建但尚未有服务器响应。
希望对您有所帮助。