我正在尝试开始为我的角度应用程序编写单元测试并快速点击停止块,因为我不确定如何以可测试的方式模拟我的服务.
有没有办法模拟REST调用,否则看起来我需要在我的测试中反映我的服务中的所有内容,这对我来说似乎不对,但我对测试写作是相当新的,所以也许这就是它的假设要完成.任何帮助将不胜感激.
我的服务如下:
angular.module('resources.users', ['ngResource'])
.factory('User', function($resource) {
var resource = $resource('/api/index.php/users/:username', {}, {
'update': {method: 'PUT'}
});
resource.getUser = function(username, successCb) {
return resource.query({username: username}, successCb);
};
return resource;
});
Run Code Online (Sandbox Code Playgroud)
我的测试到目前为止:
describe('User', function() {
var mockUserResource;
beforeEach(module('resources.users'));
beforeEach(function() {
mockUserResource = sinon.stub({
getUser: function(username) {
mockUserResource.query({username: username});
},
query: function() {}
});
module(function($provide) {
$provide.value('User', mockUserResource);
})
});
describe('getUser', function() {
it('should call getUser with username', inject(function(User) {
User.getUser('test');
expect(mockUserResource.query.args[0][0]).toEqual({username: 'test'});
}));
})
});
Run Code Online (Sandbox Code Playgroud) 我有一个简单的表格,其单元格中有很棒的图标.我使用普通的javascript使表格列可调整大小.如果溢出则隐藏单元格内容并显示省略号("..."):
td, th {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
Run Code Online (Sandbox Code Playgroud)
问题: 当调整列的大小以便隐藏内容然后再将其放大时,除了第一个之外的所有图标都消失了.
预期行为: 再次使列更大时,应重新显示图标.
请运行下面的代码段以查看它的实际效果.任何帮助高度赞赏!谢谢.
(function makeTableHeaderResizable() {
// clear resizers
Array.prototype.forEach.call(
document.querySelectorAll('.table-resize-handle'),
function(elem) {
elem.parentNode.removeChild(elem);
}
);
// create resizers
var thElm;
var startOffset;
Array.prototype.forEach.call(
document.querySelectorAll('table th'),
function(th) {
th.style.position = 'relative';
var grip = document.createElement('div');
grip.innerHTML = ' ';
grip.style.top = 0;
grip.style.right = 0;
grip.style.bottom = 0;
grip.style.width = '5px';
grip.style.position = 'absolute';
grip.style.cursor = 'col-resize';
grip.className = 'table-resize-handle';
grip.addEventListener('mousedown', function(e) {
thElm = th;
startOffset = …Run Code Online (Sandbox Code Playgroud)我想为控制器编写一个简单的测试
- 将范围的变量设置为ID
- 调用一个函数,该函数使用作用域上的ID触发API调用
- 记录结果
describe('The app', () => {
beforeEach(angular.mock.module('myModule'));
var $controller;
var eId = 123456;
beforeEach(angular.mock.inject((_$controller_) => {
$controller = _$controller_;
}));
describe('directive', () => {
it('should load the data from the api', () => {
var scope = {};
var controller = $controller('myController', { $scope: scope });
scope.entityId = eId;
expect(scope.entityId).toBe(eId);
controller.load(); // API call using scope.entityId, to load some data into the scope
scope.$digest();
console.log("entities:", controller.entities); // Where the data should be loaded into
});
});
}); …Run Code Online (Sandbox Code Playgroud) angularjs ×2
controller ×1
css ×1
css3 ×1
font-awesome ×1
html ×1
javascript ×1
unit-testing ×1