问题描述
我们有在角控制器几个方法,这些方法都不能在范围变量
有没有人知道,我们怎么能执行或致电茉莉测试里面那些方法?
下面是主要code。
VAR =的TestController TestModule.controller('的TestController',函数($范围,TestService的)
{功能handleSuccessOfAPI(数据){
如果(angular.isObject(数据))
{
$ scope.testData =数据;
}
}功能handleFailureOfAPI(状态){
的console.log(handleFailureOfAPIexecuted ::状态::+状态);
} //这是控制器的初始化函数。
功能的init(){
$ scope.testData = NULL; //部分URL
$ scope.strPartialTestURL =谐音/ testView.html; //发送测试HTTP请求
testService.getTestDataFromServer('testURI',handleSuccessOfAPI,handleFailureOfAPI);
} 在里面();
}
现在在我的茉莉花测试中,我们传递handleSuccessOfAPI和handleFailureOfAPI的方法,但这些都是不确定的。
下面是茉莉测试code。
描述('单元测试::测试控制器',函数(){
VAR范围;
VAR的TestController;VAR httpBackend;
VAR TestService的;
beforeEach(函数(){
模块('试验角角'); 注(函数($ httpBackend,_testService_,$控制器,$ rootScope){ httpBackend = $ httpBackend;
TestService的= _testService_; 范围= $ rootScope $新的()。
=的TestController $控制器('的TestController',{$适用范围:适用范围,TestService的:TestService的});
});
});afterEach(函数(){
httpBackend.verifyNoOutstandingExpectation();
httpBackend.verifyNoOutstandingRequest();
});它('测试控制器数据',函数(){
VAR URL ='测试服务器的URL; //为HTTP调用后返回,并测试设置的一些数据。
VAR returnData = {兴奋:真正}; //创建期望
httpBackend.expectGET(URL).respond(200,returnData); //拨打电话。
testService.getTestDataFromServer(URL,handleSuccessOfAPI,handleFailureOfAPI); $范围。$应用(函数(){
$ scope.runTest();
}); //刷新后端执行做expectedGET断言请求。
httpBackend.flush(); //检查结果。
//(角1.2.5后:一定要使用`toEqual`而不是`toBe`
//为对象将是一个拷贝,而不是同一个实例。)
期待(scope.testData).not.toBe(NULL);
});
});
正如您将无法访问这些功能。当你定义一个名为JS功能,它是一样的,如果你说
VAR handleSuccessOfAPI =功能(){};
在这种情况下,这将是pretty清楚的看到的变种是仅在块内的范围和不存在外部参考,将其从包装控制器。
可能被离散地称为(因此测试)的任何功能将可在控制器的$范围
We have few methods in Angular Controller, which are not on the scope variable.
Is anyone know, how can we execute or call those methods inside jasmine test?
Here is main code.
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();
}
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);
});
});
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(){};
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.
Any function which could be called discretely (and therefore tested) will be available on the $scope of the controller.
这篇关于我们如何测试非范围的角度调节器的方法呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!