gpt4 book ai didi

javascript - $提供超过 1 个值以在描述中进行测试

转载 作者:行者123 更新时间:2023-11-30 11:51:20 24 4
gpt4 key购买 nike

我有一个 Angular 指令的 Karma/Jasmine 测试:

describe('placeholder directive', function() {

// Bindable members
var element;

// Load module
beforeEach(angular.mock.module('app'));

// Mock service response
beforeEach(module(function($provide) {
$provide.value('PlaceholderSupportService', function() {
return false;
});
}));

// Bind references to global variables
beforeEach(inject(function($compile, $rootScope) {
element = $compile('<input name="test" placeholder="Test" />')($rootScope);
$rootScope.$digest();
}));

// Check the correct HTML is rendered
it('Renders placeholder as input value when placeholder is not supported', inject(function($timeout) {
$timeout.flush();
expect(element[0].value).toBe('Test');
}));

});

如我所愿。但是,我已将 PlaceholderSupportService() 的值强制设置为 false。我想运行第二个测试,我将此值为 true。我似乎无法在 it 语句中访问 $provide,那么我该怎么做呢?

最佳答案

您可以告诉您的 PlaceholderSupportService 返回一个变量。然后,在您的测试 block 上,您更改变量的值。这应该可以解决问题。这是示例:

describe('placeholder directive', function() {

// Bindable members
var element;
var providerResult = false; //Here is the variable that you will change.

// Load module
beforeEach(angular.mock.module('app'));

// Mock service response
beforeEach(module(function($provide) {
$provide.value('PlaceholderSupportService', function() {
return providerResult;
});
}));

// Check the correct HTML is rendered
it('Renders placeholder as input value when placeholder is not supported', inject(function($timeout) {
element = $compile('<input name="test" placeholder="Test" />')($rootScope);
$rootScope.$digest();
$timeout.flush();
expect(element[0].value).toBe('Test');
}));

// Check the correct HTML is rendered
it('Renders placeholder as input value when placeholder is supported', inject(function($timeout) {
providerResult = true; //Here I will change the result of my PlaceholderSupportService
element = $compile('<input name="test" placeholder="Test" />')($rootScope);
$rootScope.$digest();
$timeout.flush();
expect(element[0].value).toBe('Test');
}));

});

关于javascript - $提供超过 1 个值以在描述中进行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39374737/

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