【发布时间】:2015-09-02 11:26:39
【问题描述】:
这是我的代码
function guestDeviceManagerController(guestService, status) {
vm.initState = function() {
guestService.isUserAdmin(status.standardId).then(function(isAdmin) {
vm.isAdmin = isAdmin;
vm.template = vm.isAdmin ? vm.templates[0] : vm.templates[1];
}, function() {
//TODO: display user error
});
};
vm.initState();
}
我想知道如何模拟这个请求以及应该在 SpyOn() 中的什么位置完成,如果是这样,我需要测试返回的响应是否为 false 和 true。
做了以下修改:
describe('guestDeviceManagerController Tests', function() {
'use strict';
var scope,
controller,
statusService,
guestService,
q;
beforeEach(function() {
module('mainApp');
module('mobileDevicesModule');
inject(function($rootScope, $controller, $q, _statusService_, _guestService_) {
scope = $rootScope.$new();
statusService = _statusService_;
guestService = _guestService_;
q = $q;
controller = $controller('guestController', {
$scope: scope,
guestService: guestService
});
});
});
it('Assert view that should render for admin', function() {
spyOn(guestService, 'isUserAdmin').and.returnValue(q.when(true));
scope.$apply();
controller.initState();
expect(controller.template.url).toEqual('app/mobile-devices/guest/admin/guest.html');
});
});
现在收到以下错误:错误:意外请求:GET http://localhost:34327/guest//IsAdmin
【问题讨论】:
标签: angularjs unit-testing testing jasmine karma-jasmine