angularjs - 在 Jasmine + Angular 中测试 $httpBackend

标签 angularjs http testing jasmine httpbackend

我想我做的每件事都和教程中的一样,但有一个奇怪的错误。我想在我的 LoginService 中模拟 $http 并测试它。不幸的是,存在一些问题 - 看起来它没有看到 $httpBackend 函数。这是我的登录服务:

app.factory('LoginService', function($http) {
//..
    var validateUser = function(username, password){
        $http.post('http://domain/api/login',
        {
            username: username,
            password: password
        })
        .success(function (data) {
            //...
        });
    };

    return {
        validateUser: validateUser,
        getLoginState: getLoginState
    };
});

还有针对 validateUser 函数的测试:

describe('LoginService', function() {
var httpBackend, LoginService;
beforeEach(module('app'));

beforeEach(inject(function ($httpBackend, _LoginService_) {
    LoginService = _LoginService_;
    httpBackend = $httpBackend;
}));

afterEach(function() {
    httpBackend.verifyNoOutstandingExpectation();
    httpBackend.verifyNoOutstandingRequest();
});

it('changes loginState object when user exists', inject(function(){
    data = {username: "ut_user", token: "123456"}; 
    httpBackend.expectPOST('http://domain/api/login').respond(data);
    LoginService.validateUser("user", "pass");
    httpBackend.flush();
    expect(LoginService.getLoginState().loggedIn).toBe(true);
}));
});

在 httpBackend.verifyNoOutstandingExpectation() 上有错误;和之前在 httpBackend.flush() 上:

ReferenceError: $ is not defined
at http://localhost:9876/base/js/routes.js:21:34
at Scope.$broadcast (http://localhost:9876/base/node_modules/angular/angular.js:16200:28)
at http://localhost:9876/base/node_modules/angular/angular.js:12231:45
at Scope.$eval (http://localhost:9876/base/node_modules/angular/angular.js:15878:28)
at Scope.$digest (http://localhost:9876/base/node_modules/angular/angular.js:15689:31)
at Function.$httpBackend.verifyNoOutstandingExpectation (http://localhost:9876/base/node_modules/angular-mocks/angular-mocks.js:1563:38)
at Object.<anonymous> (http://localhost:9876/base/spec/services/loginServiceSpec.js:11:21)
at attemptSync (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1789:24)
at QueueRunner.run (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1777:9)
at QueueRunner.execute (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1762:10)

我有业力 session :

files: [
    'node_modules/angular/angular.js',
    'node_modules/angular-*/angular-*.js',
    'js/**/*.js',
    'spec/**/*.js'
],

最佳答案

你的inject不正确

beforeEach(inject(function ($httpBackend, _LoginService_) {
    LoginService = _LoginService_;
    httpBackend = $httpBackend;
}));

应该是

beforeEach(inject(function (_$httpBackend_, _LoginService_) {
    LoginService = _LoginService_;
    httpBackend = _$httpBackend_;
}));

关于angularjs - 在 Jasmine + Angular 中测试 $httpBackend,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32653648/

相关文章:

javascript - 运行 grunt 测试的 Angular 主存储库在 "bower"任务上失败

javascript - 呈现具有动态标题的表格

javascript - 在导出到 ui-grid 中的 Excel 期间设置单元格样式

java - Apache Http 组件 - 设置 cookie

php - 从url中获取所有图片

ruby-on-rails - 什么时候创建引发错误?

javascript - 了解 $resource 工厂和 @ 前缀

c# - 单声道下的 WebRequest 不支持 HTTP 协议(protocol)

javascript - HTML5 文件 API 真的会改变用户的文件上传体验吗?

testing - 如何模拟大型机器网络进行测试?