gpt4 book ai didi

angularjs - 我们如何测试非范围 Angular Controller 方法?

转载 作者:行者123 更新时间:2023-12-03 12:51:27 24 4
gpt4 key购买 nike

我们在 Angular Controller 中的方法很少,它们不在范围变量上。

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

这是主要代码。

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”方法,但这些都是未定义的。

这是 Jasmine 测试代码。
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);
});
});

最佳答案

我知道这是一个旧案例,但这是我正在使用的解决方案。

使用 Controller 的“this”

.controller('newController',['$scope',function($scope){
var $this = this;
$this.testMe = function(val){
$scope.myVal = parseInt(val)+1;
}
}]);

这是测试:
describe('newDir', function(){
var svc,
$rootScope,
$scope,
$controller,
ctrl;


beforeEach(function () {
module('myMod');
});

beforeEach(function () {
inject(function ( _$controller_,_$rootScope_) {

$controller = _$controller_;
$rootScope = _$rootScope_;
$compile = _$compile_;
$scope = $rootScope.$new();
ctrl = $controller('newController', {'$rootScope': $rootScope, '$scope': $scope });
});
});

it('testMe inc number', function() {

ctrl.testMe(10)
expect($scope.myVal).toEqual(11);
});

});

Full Code Example

关于angularjs - 我们如何测试非范围 Angular Controller 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22921484/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com