我有以下文件,我正在尝试为其编写单元测试:
import { document } from '../../globals';
const Overlay = () => {
console.log(document.getElementsByTagName()); // this outputs 'undefined'
};
Run Code Online (Sandbox Code Playgroud)
我正在努力模拟该getElementsByTagName功能。我的测试看起来像这样。
import { document } from '../../globals';
jest.mock('../../globals', () => ({
document: {
getElementsByTagName: jest.fn().mockReturnValue('foo')
}
}));
console.log(document.getElementsByTagName()); // this outputs 'foo'
Run Code Online (Sandbox Code Playgroud)
但不幸的console.log是,顶部文件中的 总是输出undefined. 它可以看到文档对象和getElementsByTagName模拟,但返回值始终是undefined。
如果我console.log(document.getElementsByTagName)得到以下信息:
{ getElementsByTagName:
{ [Function: mockConstructor]
_isMockFunction: true,
getMockImplementation: [Function],
mock: [Getter/Setter],
mockClear: [Function],
mockReset: [Function],
mockReturnValueOnce: [Function],
mockReturnValue: [Function],
mockImplementationOnce: [Function],
mockImplementation: [Function], …Run Code Online (Sandbox Code Playgroud) 我有一个angularJS $资源:
$resource("http://localhost:3000/:id",{
id: '@id'
},
{
get: {
method:'GET',
isArray: false
},
foo: {
method:'POST',
url: 'http://localhost:3000/:id/foo',
isArray: false
}
});
Run Code Online (Sandbox Code Playgroud)
现在,如果我打电话:
User.foo({id:'123', anotherParam: 'bar'});
Run Code Online (Sandbox Code Playgroud)
这会导致调用URL" http:// localhost:3000/foo "并将id和anotherParam参数作为POST字段传递.
我实际上希望它调用' http:// localhost: 3000/123 / foo '并仅将anotherParam参数作为POST字段传递.
如何让id参数正常运行?
我在Express中编写了一系列REST API,并希望编写一种强大而一致的方法来处理错误并将其呈现给用户.到目前为止,我使用的中间件如下所示:
app.use(function(req, res, next){
res.render('404', { status: 404, error: 'not_found', error_description: 'the url '+req.url+' could not be found' });
});
app.use(function(err, req, res, next) {
switch (err.name) {
case 'CastError':
res.status(400);
return res.send({code:400, error: 'cast_error', error_description: err});
default:
console.log(err);
return res.send({code:500, error: 'internal_error', error_description: 'something went horribly wrong!'});
}
});
Run Code Online (Sandbox Code Playgroud)
现在问题在于错误可能来自应用程序的任何部分.例如,Mongoose会触发"CastError"之类的错误,可能会出现一般系统错误(无法连接到数据库),还会出现用户验证错误.
现在有些错误我想向用户显示一条消息(例如验证错误或告诉他们API请求有什么问题),而其他我想要隐藏用户的错误.
所以看一下验证错误:
throw new Error('not a valid project id');
Run Code Online (Sandbox Code Playgroud)
此刻,它只是作为一般500错误而被捕获,并且没有向用户显示有用的消息.如何将上述错误与一般系统错误区分开来,以便对它们进行不同的处理?是否有我可以使用的错误类型属性(可能是err.name).如果是这样,我该如何使用它?
概念创意(obv不起作用):
throw new Error('validation', 'not a valid project id');
Run Code Online (Sandbox Code Playgroud)
要么
throw new Error('not a valid project id').name('validation');
Run Code Online (Sandbox Code Playgroud) 我有来自数据库的以下字符串:让我们获得功能
如果我通过 strlen 运行它,它会返回 25 个字符,而不是预期的 20 个。var 转储显示该字符串与上面类似(没有 html 引用等)。
如果我删除单引号,strlen 将返回 19 个字符。
显然,引用返回 5 个字符而不是 1 个 - 为什么?我怎样才能阻止这个?
谢谢!
我正在尝试在ng-repeat内部获得一个指令来工作..当它在HTML中被硬编码但是在切换到ng-repeat之后它工作的某些方面停止工作.
<div ng-repeat="section in filterSections" filter-tray>
Test {{section.label}}
</div>
Run Code Online (Sandbox Code Playgroud)
我有一个带有控制器的模块,可以发出事件:
controller: function($scope, $element) {
this.activateTray = function(trayID) {
$scope.$emit('filterTray::show', {
tray: trayID
});
};
};
Run Code Online (Sandbox Code Playgroud)
我在页面上有一个指令 - 它应该从控制器接收事件.由于切换到使用ng-repeat接收事件已停止工作.它仍然初始化,它只是不听事件.
.directive('filterTray', function() {
return {
restrict: 'A',
require: '^filter',
link: function($scope, $element, attrs, filterNavController) {
console.log('this debug statement works');
$scope.$on('filterTray::show', function(e, data) {
console.log('this debug statement never runs');
});
}
};
})
Run Code Online (Sandbox Code Playgroud)
由于添加重复,$ scope变量受到影响?也许$ on不再听正确的事了吗?任何想法/提示将不胜感激!