gpt4 book ai didi

javascript - 实例 stub 方法

转载 作者:搜寻专家 更新时间:2023-10-31 23:40:10 25 4
gpt4 key购买 nike

我有一个使用 node-slack-sdk 的 Express 应用程序当某些端点被击中时向 Slack 发帖。我正在尝试为一条路线编写集成测试,其中包括调用该库中的方法。

我想阻止 Slack 库中某些方法的所有默认行为,并简单地断言这些方法是用某些参数调用的。

我试图简化问题。我怎样才能 stub WebClient 实例的方法(实际上嵌套在 chat 中),阻止原始功能,并断言调用它的参数?

我已经尝试了很多没有用的东西,所以我正在编辑它并在此处提供一个大大简化的设置:

index.html:

const express = require('express');
const {WebClient} = require('@slack/client');
const app = express();
const web = new WebClient('token');

app.post('/', (req, res) => {

web.chat.postMessage({
text: 'Hello world!',
token: '123'
})
.then(() => {
res.json({});
})
.catch(err => {
res.sendStatus(500);
});
});

module.exports = app;

index.test.html

'use strict';
const app = require('../index');
const chai = require('chai');
const chaiHttp = require('chai-http');
const sinon = require('sinon');

const expect = chai.expect;
chai.use(chaiHttp);

const {WebClient} = require('@slack/client');


describe('POST /', function() {
before(function() {
// replace WebClient with a simplified implementation, or replace the whole module.
});

it('should call chat.update with specific arguments', function() {
return chai.request(app).post('/').send({})
.then(function(res) {
expect(res).to.have.status(200);
// assert that web.chat.postMessage was called with {message: 'Hello world!'}, etc
});
});
});

与其他示例不同,有几件事使这变得困难。第一,我们无权访问测试中的 web 实例,因此我们无法直接 stub 方法。第二,该方法隐藏在 chat 属性 web.chat.postMessage 中,这也不同于我在 sinon、proxyquire 等文档中看到的其他示例。

最佳答案

您的示例设计不是很容易测试,这就是您遇到这些问题的原因。为了使其更具可测试性和内聚性,最好传入 WebClient 对象和其他依赖项,而不是在路由中创建它们。

const express = require('express');
const {WebClient} = require('@slack/client');
const app = express();//you should be passing this in as well. But for the sake of this example i'll leave it


module.exports = function(webClient) {
app.post('/', (req, res) => {

web.chat.postMessage({
text: 'Hello world!',
token: '123'
})
.then(() => {
res.json({});
})
.catch(err => {
res.sendStatus(500);
});
})
return app;
};

为了实现这个,在更高的模块中构建你的对象/路由。 (您可能必须编辑 express 为您生成的内容。我不确定,我个人使用经过大量重构的 express 版本来满足我的需要。)通过传入您的 WebClient,您现在可以为您的测试创建一个 stub 。

'use strict';

const chai = require('chai');
const chaiHttp = require('chai-http');
const sinon = require('sinon');

const expect = chai.expect;
chai.use(chaiHttp);
const {WebClient} = require('@slack/client');
const web = new WebClient('token');
let app = require('../index')(web);

describe('POST /', function() {

it('should call chat.update with specific arguments', function() {
const spy = sinon.spy();
sinon.stub(web.chat, 'postMessage').callsFake(spy);

return chai.request(app).post('/').send({})
.then(function(res) {
expect(res).to.have.status(200);
assert(spy.calledWith({message: 'Hello world!'}));
});
});
});

这被称为 Dependency Injection .不是让你的索引模块构建它的依赖项,WebClient,你的高级模块将传递依赖项,以便它们控制它的低级模块的状态。您的上层模块,您的测试,现在拥有为下层模块 index 创建 stub 所需的控制权。

上面的代码只是快速的工作。我还没有测试它是否有效,但它应该可以回答你的问题。

关于javascript - 实例 stub 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49950653/

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