当前位置: 首页 > 知识库问答 >
问题:

实例的存根方法

凌波峻
2023-03-14

我有一个使用node-slack-sdk的Express应用程序,当某些endpoint被击中时,它会使帖子松弛。我正在尝试为一个路由编写集成测试,其中包括调用该库中的方法。

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

我试图把问题简单化。如何建立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;
'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
        });
    });
});

共有1个答案

魏宸
2023-03-14

您的示例的设计不是非常可测试的,这就是为什么您有这些问题。为了使其更具可测试性和内聚性,最好传入您的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,现在可以为测试创建存根。

'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!'}));
        });
    });
});

这称为依赖注入。不是让索引模块构建它的依赖项WebClient,而是让较高的模块传入依赖项,以便它们控制它的较低模块的状态。您的较高模块,即您的测试,现在拥有了为较低模块,即索引创建存根所需的控件。

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

 类似资料:
  • 问题内容: 这是我要做的: 为下面的main()调用的方法定义存根。每个存根应打印“ FIXME:完成methodName()”,后跟换行符,并应返回-1。 输出示例: 这是我的代码: 我以为我理解方法存根,但是我感觉自己犯了一个非常愚蠢而简单的错误?我只能编辑代码的公共static int methodName部分。 问题答案: 从这个意义上讲,方法存根是没有实质内容的方法,即它没有按照计划执行

  • 如果我已经通过< code > var a = sinon . createstuinstance(my contractor)创建了一个实例。 如何替换其中一个存根函数,如 。 我这样做的主要原因是想实现这个提到的多个回调解决方法

  • 当调用Person class by [name = "Mohammad ",age = 26]的实例时,我想返回4。当调用Person class by [name = "Ali ",age = 20]的实例时,我想返回5。 所以我有这些课程: 我的刀类: 这是计算器类 这是我的测试: 那么为什么我的测试会失败呢?

  • 问题内容: 考虑以下代码: 编译器抱怨正在尝试对行为进行存根的行。关于如何使用存根方法返回带有有界通配符的类型的任何指针? 问题答案: 您也可以为此使用非类型安全方法doReturn, 正如在Mockito的Google组中讨论的那样。 尽管它比起来简单,但再次请注意,它不是类型安全的。如果您担心类型安全,那么米尔豪斯的答案是正确的。 额外细节 要明确的是,这是观察到的编译器错误, 我相信编译器已

  • 我正在尝试使用codeception和他的Mock和Stub库运行测试。发生的事情是,当我试图用存根运行测试时,它引发了一个在我的实际类中实现的错误。 我认为存根是隔离测试和模拟真实对象的方法。为什么它叫我真正的方法? 所以当我运行代码欺骗(PHPcodeception.phar运行): 问题是: 回溯会一直持续到类LoadAVG的真正实现,在这里我真正调用了rewind()函数,很明显,一旦这个

  • 问题内容: 我正在尝试使用sinon.js存根方法,但是出现以下错误: 这是我的代码: 这是上述代码的jsFiddle(http://jsfiddle.net/pebreo/wyg5f/5/),还有我提到的SO问题的jsFiddle(http://jsfiddle.net/pebreo/9mK5d/1/)。 我确保在ssfiddle甚至jQuery 1.9 的 外部资源 中都包含了sinon 。我