【问题标题】:Unable to access scope variable in jasmine test case无法访问 jasmine 测试用例中的范围变量
【发布时间】:2015-10-12 15:04:44
【问题描述】:

我的模块中定义了一个控制器,如下所示,

myTestApp.controller("LoginController", function($scope,$http,$location) {

$scope.submitForm = function(){ 

    var userEmail = $scope.user.email;
    var userPassword = $scope.user.password;

    $http({
        url:'api/login/',
        headers: {'Content-Type': 'application/json'},
        method:'POST',
        data:{
            username:userEmail,
            password:userPassword
        }})
        .success(function(data){
            $scope.loginResult = data;
        });
     };   
});

我正在尝试使用 httpBackend 编写一个模拟 HTTP 调用的测试用例。

这是我的jasmine 测试代码:

 describe('make HTTP mock call', function() {

var scope, httpBackend, http, controller;

beforeEach(module('myTestApp'));

describe('with httpBackend', function() {

    beforeEach(inject(function ($rootScope, $controller, $httpBackend, $http) {
        scope = $rootScope.$new();
        httpBackend = $httpBackend;
        http = $http;
        controller = $controller('LoginController', {$scope : scope} );
        httpBackend.when("POST", "api/login/",{'username':'test@gmail.com', 'password':'test'}, function(headers) {
           return {
            'Content-Type': 'application/json',
           };
        }).respond(200);
    }));

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

    it('to check if user is valid', inject(function ($http) {   

        var expectedResponse = { success: true };

        httpBackend.expectPOST('api/login/', {
            "username": "test@gmail.com",
            "password": "test"
        }, function(headers){
            return headers['Content-Type'] === 'application/json';
        }).respond(function(method, url, data, headers, params){
            return [200, {}, {}];
        });         

        //actual HTTP call
        $http({
        url:'api/login/',
        headers: {'Content-Type': 'application/json'},
        method:'POST',
        data:{
            username:"test@gmail.com",
            password:"test"
        }});

        //flush response
        httpBackend.flush();
        expect(scope.loginResult).toEqual(expectedResponse);
    }));    
  });

});

但是,当我运行测试用例时,出现以下错误

预期未定义等于 Object({ success: true })。

我想将我的 HTTP 调用的响应数据与 expectedResponse 进行比较,如果两者相等,则测试应该通过。

但是,我无法从我的测试用例中访问 $scope 变量 loginResult。谁能告诉我我在这里做错了什么?

有人可以描述使用 Jasmine 模拟 HTTP 调用的最佳方法吗?

【问题讨论】:

    标签: javascript angularjs jasmine


    【解决方案1】:

    只有在调用函数submitForm 时才会实际填充您的登录数据。所以在测试中,在做flush()之前调用函数。

    更新: 确保你已经定义了

    $scope.user={email :'test@gmail.com,password:'test'} 
    

    在测试的第一行(就在it('to check if user is valid', inject(function ($http) { 之后)

    【讨论】:

    • 嗨,当我调用 submitForm 时,我收到错误 TypeError: Cannot read property 'email' of undefined
    • 这是因为在您的范围内,请确保您已在测试的第一行定义了 $scope.user={email :'test@gmail.com,password:'test'}(就在 it('to check if user is valid', inject(function ($http) { 之后)。
    • @ParthDoshi 欢迎您,将解决方案的第二部分添加到原始答案中
    猜你喜欢
    • 2016-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多