Javascript 控制台日志sinon.spy()显示[Function],而不是spy的内容
我试图查看Javascript 控制台日志sinon.spy()显示[Function],而不是spy的内容,javascript,node.js,mocha.js,sinon,chai,Javascript,Node.js,Mocha.js,Sinon,Chai,我试图查看sinon.spy()的输出,但是当我执行console.log(res.render)时,我在运行测试时只在控制台中看到[Function] const should = require( 'chai' ).should() const sinon = require( 'sinon' ) const getIndex = ( req, res ) => { res.render( 'index' ) } describe( 'getIndex', function (
sinon.spy()
的输出,但是当我执行console.log(res.render)
时,我在运行测试时只在控制台中看到[Function]
const should = require( 'chai' ).should()
const sinon = require( 'sinon' )
const getIndex = ( req, res ) => {
res.render( 'index' )
}
describe( 'getIndex', function () {
it( 'should render index', function () {
let req = {}
let res = {
render: sinon.spy()
}
getIndex( req, res )
console.log( res.render ) // Expecting all properties from sinon.spy()
res.render.firstCall.args[ 0 ]
.should
.equal( 'index' )
} )
} )
编辑:
我希望看到sinon.spy()
中提供的所有属性。例如,firstCall.args[0]
属性,但我想查看所有属性。我知道它们列在中,但我希望一次看到所有属性,因为我正试图更好地了解sinon如何进行测试。- 当您单击
部分时(如果您至少在chrome开发控制台中),它将显示函数代码的内容,而不是结果函数
- 这是因为
不是字符串或数字或其他任何东西,它是一个函数res.render
- 因此,我们需要某种方法记录
的输出res.render
- 如果调用
并将其必要的参数传递给它,我们将得到res.render
的输出res.render()
- TL;DR我会尝试类似于
的方法,而不仅仅是console.log(res.render(“index”))
console.log(res.render)
- 间谍是一种功能
- 开发工具控制台或监视概述不显示函数对象的属性
console.log()
在inspector中显示属性的原因是,开发工具知道它的a函数,并且函数也是这样显示的
开发工具
开发工具显然假定Function
对象(通常)没有特殊属性,因此不会枚举它们
您也可以通过基本功能观察到这一点。在控制台中输入以下内容:
var f = function() { }
f.foo = 42;
f; // The property won't be printed
不过,当在断点处停止时,您可以在调试器中检查spy对象。您可以在“源”选项卡中添加对“监视”面板的引用。然后可以打开函数对象并检查属性
在前面的实验中,您确实可以通过为f
添加一个watch表达式来检查函数
正在检查的Sinon间谍对象:
您的期望是什么?请编辑您的问题以添加预期结果。请通过编辑问题向您的问题添加注释。使用帖子下方的编辑按钮。在那之后删除你的评论。你说的有道理,但我认为这不适用于sinon在本案中的情况。spy应该包含
getIndex(req,res)
中调用的信息,其中res
是spy,并作为res.render('index')
传递。它确实在测试中成功返回,但我正在寻找间谍得到的关于这种呼叫的信息。