Angularjs 使用$httpBackend进行Karma测试-没有待处理的刷新请求
前言:我有两种方法,分别称为Angularjs 使用$httpBackend进行Karma测试-没有待处理的刷新请求,angularjs,coffeescript,jasmine,karma-runner,httpbackend,Angularjs,Coffeescript,Jasmine,Karma Runner,Httpbackend,前言:我有两种方法,分别称为injections和controllerInjections,它们简单地简化了测试中的注入。它将注入放在这个中 我的测试如下所示: describe "user profile controller", -> beforeEach module(APP_NAME) beforeEach module('ngMockE2E') afterEach => @$httpBackend.verifyNoOutstandingExpectati
injections
和controllerInjections
,它们简单地简化了测试中的注入。它将注入放在这个中
我的测试如下所示:
describe "user profile controller", ->
beforeEach module(APP_NAME)
beforeEach module('ngMockE2E')
afterEach =>
@$httpBackend.verifyNoOutstandingExpectation()
@$httpBackend.verifyNoOutstandingRequest()
$stateParams =
slug: 'some-person'
user =
id: 1566
slug: 'some-person'
full_name: 'Some person'
# this method takes a callback when injections are done
injections.call @, '$httpBackend', =>
@$httpBackend
.whenGET(/\/api\/users\/([a-z0-9\-\_]+)\/?/)
.respond [200, user]
# @$httpBackend.expectGET("/api/users/#{$stateParams.slug}")
@$httpBackend
.whenGET(/^\/views\//)
.respond 200, ''
# this method takes extra parameters to push into the controller
controllerInjections.call @, 'UserProfileController', {$stateParams}
it "should load user info when requested", =>
resp = null
@scope.$apply =>
resp = @UserProfileController.loadUserInfo()
# console.debug 'user', @UserProfileController.user
# debugger
console.debug 'resp', resp
@$httpBackend.flush()
当调用flush()
方法时,我不断得到“无待处理的刷新请求!”。但是当我删除/api/users/..
的whenGET
时,我得到了一个“意外请求”,这意味着该请求肯定正在被调用。所以flush()
当我取消注释expectGET
调用时,在测试结束时,我似乎没有突出的期望
@UserProfileController.loadUserInfo()
用于使用$http
访问API,并在完成时进行回调。回调永远不会运行,尽管请求似乎已发送(从调试器中看到),但未收到响应
有什么想法吗