document.getElementById(“username”)的jasmine代码.focus()

时间:2017-04-03 08:50:25

标签: angularjs jasmine karma-jasmine

我面临以下错误

  

Typerror:无法读取myapp.controller ('MyController',fucntion ($scope){ document.getElementById("userame").focus(); .... });

的属性焦点

示例源代码

document.getElementById()...

请为literal.Text = tableData.ToString(); literal2.Text = tableData2.ToString(); literal3.Text = tableData3.ToString();

建议茉莉花代码

1 个答案:

答案 0 :(得分:2)

问题已解决。请在下面找到示例代码。

描述( 'myController的',函数(){

    var $location,$rootScope,$http,$window,LoginObj,scope;
    beforeEach(inject(
            function( _$location_,_$rootScope_,_$http_,_$window_,_LoginObj_) {
                $location = _$location_;
                $rootScope = _$rootScope_;
                $http = _$http_;
                $window=_$window_;
                LoginObj = _LoginObj_;
           }));
    it('history.go(1) ',function(){
        //creating dummy element for html element 
        var dummyElement = document.createElement('div');
        scope = $rootScope.$new(); 

        document.getElementById = jasmine.createSpy('HTML Element').and.returnValue(dummyElement);
        var controller = $controller('MyController',{
            $scope : $scope
        });
        //checking the focus of username
        spyOn(document.getElementById("username"),'focus');
        expect(document.getElementById("username").focus).toBeDefined();