- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在寻找使用 sinon stub 来测试下面的方法链:
driver.manage().window().setSize()
我发现了一个相关问题,它解释了如何访问链下的一种方法,但这似乎并没有让我访问任何其他方法。
t.context.webdriver = sinon.stub(new WebDriver)
sinon.stub(t.context.webdriver, "manage", () => {
return {
window: sinon.stub().returns();
};
})
返回错误
Error: this._driver.manage(...).window(...).setSize is not a function
如何 stub 多级方法链?
最佳答案
我不确定您要测试什么,但错误是因为您的 stub 返回的对象没有 window()
函数或 设置大小()
。链之所以起作用,是因为链的每个部分都返回一些与下一次调用匹配的方法。所以如果你在链的早期填充一些东西,你需要确保你返回的东西有那些方法。也许这涉及传回原始返回,或者您可能伪造整个链条。
这是一个至少不会抛出的例子:
const sinon = require('sinon')
// some fake object that maybe looks like what you have
let driver = {
manage(){ return this},
window() { return this},
setSize() {console.log("size set")}
}
// stubb manage and now you're resposible for the whole chain
sinon.stub(driver, "manage").callsFake(() => {
console.log("called")
return {
window(){
return { setSize: sinon.stub().returns() }
}
};
})
当然,根据您要测试的内容,可能会有很多变化。
关于javascript - Sinon Stub JavaScript 方法链,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51682449/
我有一个正在 stub 的函数,该函数使用多个参数进行调用。我想检查仅第一个参数。其余的都是回调函数,所以我不想管它们。因此,我可能有以下 2 个调用,以 ajax 为例: method.get =
我试图用 Sinon.js 拦截一个调用,这样我就可以做一些日志记录,然后执行原始调用。我看不到用 sinon.spy() 做到这一点的方法,但我想我可以用 sinon.stub() 做到这一点。 我
我是SinonJs的初学者,当我尝试编写一些演示代码时,它无法工作,我不知道为什么。 应用程序.js const db = require('./db'); module.exports.si
sinon-mongoose = 2.3.0不适用于 sinon 9我的代码如下 // test.js const sinon = require('sinon'); require('sinon-m
我正在尝试创建一个单元测试来测试我的代码创建的对象是否已成功发送到更新函数(当前不测试更新函数,因为这将是集成测试)。 我想做的是使用 sinon 模拟来检查更新方法的输入是否正确: var obje
当使用 sinon 时,我只想替换函数的返回值,不需要其他信息,例如调用了多少次。哪一个更好? sinon.replace(Component.prototype, 'getValue', () =>
我在获取一个 sinon stub 来返回/解析另一个 sinon stub 时遇到问题。我正在使用 sinon、chai、chai-as-promised 和 mocha。 我正在按顺序执行多个异步
在我们的前端单元测试中使用 sinon 和 sinon-qunit,我很难理解这些方法的区别。我们正在使用 sinon.sandbox.stub()(字面意思是函数,我们不创建沙箱)并且这些 stub
我正在尝试调用 fail我的片段中的条件。 但是当我使用 sinon.stub().throws()方法它显示我错误。 我无法在代码中处理它。 这是我的片段: login() { let lo
我有一个中间件功能,它检查 session token 以查看用户是否是管理员用户。如果所有检查都通过,该函数不会返回任何内容,而只是调用 next()。 在对 Sinon spy 的 next()
我想在node.js和mocha上使用sinon。我在测试文件中执行以下操作: var sinon = require('sinon') 之后 sinon var 是空对象。 global.sinon
在下面的单元测试代码中: TestModel = Backbone.Model.extend({ defaults: { 'selection': null },
当我在对象内部的函数上使用 Sinon 时,它可以工作: function myFunc() { console.log('hello'); } var myObj = { myFunc: m
假设我想通过添加我自己的特殊 stub 来扩展 sinon:我可以这样写: sinon.specialStub = function() { return this.stub().return
我有一个函数需要使用 sinon 进行测试。它需要两个参数并具有可以引发的不同事件。我正在尝试模拟“准备就绪”事件,以模拟成功的 SFTP 连接 function configureSFTPConne
我有一个非常简单的 JS 库(称为 trysinon.js),如下所示: export function foo() { bar(); } export function bar() { ret
我们正在使用Sinon.JS对于一些集成测试,我遇到了一个问题,我需要以某种方式进行调试。我们使用 fakeServer.create() 方法,如下所示: server = Sinon.fakeSe
我发现了几个相关的问题,但似乎没有一个对我想要实现的内容有帮助。 因此,我想监视构造函数方法,以便当使用构造函数创建的对象在不同范围另一个函数中调用此方法时,我可以知道该调用所使用的参数制作。 示例:
我正在尝试为这个简单的中间件功能编写一个独立的测试 function onlyInternal (req, res, next) { if (!ReqHelpers.isInternal(req)
我觉得我大致了解 sinon 的工作原理,但在监视传递给我正在测试的导入函数的回调时遇到了一些麻烦。这是一个例子: import sinon from 'sinon' const callbackCa
我是一名优秀的程序员,十分优秀!