sách gpt4 ai đã đi

Kiểm tra AngularJS - Kiểm tra typeahead của AngularUI bằng cách sử dụng $http và then()

In lại 作者:行者123 更新时间:2023-11-29 22:07:16 29 4
mua khóa gpt4 Nike

我在测试我与 typeahead.js ( https://github.com/angular-ui/bootstrap/blob/master/src/typeahead/typeahead.js ) 一起使用的函数时遇到问题。我通常知道如何解决测试中的 promise ,但不知道如何使用以下功能:

$scope.getSuggestion = function ( name, length ) {
return $http.get( 'api/autocomplete/?contains=' + name )
.then( function ( response ) {
return response.data.slice( 0, length || 7 );
});
};

我的测试是这样的:

describe('Suggestions', function () {
it('should be possible to get suggestions', function () {
$httpBackend.expectGET('api/autocomplete?title__contains=Foo').respond([
{ name: 'Foobar' },
{ name: 'Foobala' },
{ name: 'Foolooloo' }
]);
var suggestions = $scope.getSuggestion( 'Foo' );
$rootScope.$apply();

// Here should be a test.
console.log(suggestions);
})
});

Nhưngsuggestion只是promise对象Object{then: function (callback, errback) { ... }}.

我哪里搞砸了!?

1 Câu trả lời

suggestions 是一个 promise,它KHÔNG一个实际值,你需要调用 then() 来获取它的值.也就是

suggestions.then(function(data) {
// Here should be a test.
console.log(data);
});

làm mới:

Hãy thử cách này:

describe('Suggestions', function () {
it('should be possible to get suggestions', function () {
$httpBackend.expectGET('api/autocomplete?title__contains=Foo').respond([
{ name: 'Foobar' },
{ name: 'Foobala' },
{ name: 'Foolooloo' }
]);

var suggestions;

$scope.getSuggestion( 'Foo' ).then(function(data) {
suggestions = data;
});

$httpBackend.flush();
$rootScope.$apply(); // might be optional

// Here should be a test.
console.log(suggestions);
})
});

关于javascript - AngularJS 测试 - 使用 $http 和 then() 测试 AngularUI 的提前输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20245778/

29 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com