我们如何测试非作用域角度控制器方法? [英] How can we test non-scope angular controller methods?

查看:18
本文介绍了我们如何测试非作用域角度控制器方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在 Angular Controller 中有几个方法,它们不在作用域变量上.

We have few methods in Angular Controller, which are not on the scope variable.

有谁知道,我们如何在 Jasmine 测试中执行或调用这些方法?

Does anyone know, how we can execute or call those methods inside Jasmine tests?

这是主要代码.

var testController = TestModule.controller('testController', function($scope, testService)
{

function handleSuccessOfAPI(data) {
    if (angular.isObject(data))
    {
       $scope.testData = data;
    }
}

function handleFailureOfAPI(status) {
    console.log("handleFailureOfAPIexecuted :: status :: "+status);
}

 // this is controller initialize function.
 function init() {
    $scope.testData = null; 

    // partial URL
    $scope.strPartialTestURL = "partials/testView.html;

    // send test http request 
    testService.getTestDataFromServer('testURI', handleSuccessOfAPI, handleFailureOfAPI);
}

 init();
}

现在在我的 jasmine 测试中,我们正在传递handleSuccessOfAPI"和handleFailureOfAPI"方法,但这些方法未定义.

Now in my jasmine test, we are passing "handleSuccessOfAPI" and "handleFailureOfAPI" method, but these are undefined.

这里是茉莉花测试代码.

Here is jasmine test code.

describe('Unit Test :: Test Controller', function() {
var scope;
var testController;

var httpBackend;
var testService;


beforeEach( function() {
    module('test-angular-angular');

    inject(function($httpBackend, _testService_, $controller, $rootScope) {

        httpBackend = $httpBackend;
        testService= _testService_;

        scope = $rootScope.$new();
        testController= $controller('testController', { $scope: scope, testService: testService});
            });
});

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

it('Test controller data', function (){ 
    var URL = 'test server url';

    // set up some data for the http call to return and test later.
    var returnData = { excited: true };

    // create expectation
    httpBackend.expectGET(URL ).respond(200, returnData);

    // make the call.
    testService.getTestDataFromServer(URL , handleSuccessOfAPI, handleFailureOfAPI);

    $scope.$apply(function() {
        $scope.runTest();
    });

    // flush the backend to "execute" the request to do the expectedGET assertion.
    httpBackend.flush();

    // check the result. 
    // (after Angular 1.2.5: be sure to use `toEqual` and not `toBe`
    // as the object will be a copy and not the same instance.)
    expect(scope.testData ).not.toBe(null);
});
});

推荐答案

照原样,您将无法访问这些功能.当你定义一个命名的 JS 函数时,就像你要说的一样

As is you won't have access to those functions. When you define a named JS function it's the same as if you were to say

var handleSuccessOfAPI = function(){};

在这种情况下,很明显可以看到 var 仅在块内的范围内,并且没有来自包装控制器的外部引用.

In which case it would be pretty clear to see that the var is only in the scope within the block and there is no external reference to it from the wrapping controller.

任何可以被离散调用(并因此被测试)的函数都将在控制器的 $scope 中可用.

Any function which could be called discretely (and therefore tested) will be available on the $scope of the controller.

这篇关于我们如何测试非作用域角度控制器方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆