Jasmine Object#<scope>没有方法</scope>

时间:2014-02-21 22:15:43

标签: javascript angularjs testing jasmine

我一直收到错误:Object <Scope> has no method我已经环顾四周并尝试了一切。

这是我的茉莉花:

var scope, service1, service2;
var EmployeeService_mock = function(){
        var obj = {
            visibleEmployees : [],
            selectedEmployee : null
        };
        obj.setSelectedEmployee = function (emp) {
            obj.selectedEmployee = emp;
        };
        return obj;
    };
    var EmpTreeNavigationService_mock = function(){

    };

    beforeEach(inject(function($rootScope, $controller){
        scope = $rootScope.$new();
        service1 = new EmployeeService_mock();
        service2 = new EmpTreeNavigationService_mock();
        $controller('EmployeesCtrl',
            {
                $scope: scope,
                EmployeeService: service1,
                EmpTreeNavigationService: service2
            }
        );
        scope.$digest();
    }));

    it('should test', function(){
        expect(scope.isSelectedEmployee(emp1)).toBeTruthy();   
    });

这是我的.js

$scope.isSelectedEmployee = function (emp) {
    return true;
};

我似乎无法弄清楚这一点。我仔细检查了我的拼写,查看了其他示例,这应该有效。

1 个答案:

答案 0 :(得分:0)

我可以看到的最大问题是您忘记在测试中加载模块。像这样:

beforeEach(module('yourModule'));

看看这个working demonot working demo(它有完全相同的问题)