gpt4 book ai didi

angularjs - 如何测试 Controller 内服务的响应

转载 作者:行者123 更新时间:2023-12-01 09:52:19 24 4
gpt4 key购买 nike

我正在尝试测试在 Controller 中执行的服务 http 调用的响应:

Controller :

define(['module'], function (module) {
'use strict';

var MyController = function ($scope, MyService) {

var vm = this;

$scope.testScope = 'karma is working!';

MyService.getData().then(function (data) {
$scope.result = data.hour
vm.banner = {
'greeting': data.greeting
}
});
};

module.exports = ['$scope', 'MyService', MyController ];
});

单元测试:

define(['require', 'angular-mocks'], function (require) {
'use strict';

var angular = require('angular');

describe("<- MyController Spec ->", function () {

var controller, scope, myService, serviceResponse;

serviceResponse= {
greeting: 'hello',
hour: '12'
};

beforeEach(angular.mock.module('myApp'));

beforeEach(inject(function (_$controller_, _$rootScope_, _MyService_, $q) {
scope = _$rootScope_.$new();
var deferred = $q.defer();
deferred.resolve(serviceResponse);

myService = _MyService_;
spyOn(myService, 'getData').and.returnValue(deferred.promise);

controller = _$controller_('MyController', {$scope: scope});
scope.$apply();
}));

it('should verify that the controller exists ', function() {
expect(controller).toBeDefined();
});

it('should have testScope scope equaling *karma is working*', function() {
expect(scope.testScope ).toEqual('karma is working!');
});
});
});

我如何测试 http 请求是否执行并返回绑定(bind)到 $scope.resultvm.banner 的 serviceResponse问候语


我试过:

define(['require', 'angular-mocks'], function (require) {
'use strict';

var angular = require('angular');

describe("<- MyController Spec ->", function () {

var controller, scope, myService, serviceResponse, $httpBackend;

serviceResponse= {
greeting: 'hello',
hour: '12'
};

beforeEach(angular.mock.module('myApp'));

beforeEach(inject(function (_$controller_, _$rootScope_, _MyService_, _$httpBackend_) {
scope = _$rootScope_.$new();
$httpBackend = _$httpBackend_

$httpBackend.expectGET("/my/endpoint/here").respond(serviceResponse);

myService = _MyService_;
spyOn(myService, 'getData').and.callThrough();

controller = _$controller_('MyController', {$scope: scope});
scope.$apply();
}));

it('should call my service and populate scope.result ', function() {
myService.getData();
expect(scope.result ).toEqual(serviceResponse.hour);
});

it('should verify that the controller exists ', function() {
expect(controller).toBeDefined();
});

it('should have testScope scope equaling *karma is working*', function() {
expect(scope.testScope ).toEqual('karma is working!');
});
});
});

出现错误:

[should call my service and populate scope.result -----     Expected undefined to be defined.

最佳答案

我认为问题是你的服务返回了一个 promise ,所以当你这样做时it

it('should call my service and populate scope.result ', function() {
myService.getData();
expect(scope.result ).toEqual(serviceResponse.hour);
});

你的服务可能在它到达expect之前还没有被解析,所以你必须先等待你的promise的then然后在里面做expect .你可以做的是将 promise 分配给 $scope.result

var MyController = function ($scope, MyService) {

var vm = this;

$scope.testScope = 'karma is working!';

$scope.result = MyService.getData().then(function (data) {
$scope.result = data.hour
vm.banner = {
'greeting': data.greeting
}
});
};

然后在你的测试中,你可以做类似的事情

it('should call my service and populate scope.result ', function() {
//myService.getData(); <- you don't have to call this
scope.result.then(function(){
expect(scope.result).toEqual(serviceResponse.hour);
});

});

您将需要模拟 $httpBackend 并期望特定请求并提供模拟响应数据。这是 Angular docs 的片段

beforeEach(inject(function($injector) {
// Set up the mock http service responses
$httpBackend = $injector.get('$httpBackend');

// backend definition common for all tests
$httpBackend.when('GET', '/auth.py')
.respond({userId: 'userX'}, {'A-Token': 'xxx'});

}));

现在,每当 $http 调用 /auth.py 时,它都会用模拟数据 {userId: 'userX'}, {'A-Token': ' xxx'}

关于angularjs - 如何测试 Controller 内服务的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35648484/

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