简体   繁体   English

Sinon-Stub模块功能,无需依赖注入即可对其进行测试

[英]Sinon - Stub module function and test it without dependency injection

I have a proxy module, which forwards function calls to services. 我有一个代理模块,它将功能调用转发给服务。 I want to test if the service function is called, when a function in this proxy module is called. 我想测试在调用此代理模块中的函数时是否调用了服务函数。

Here is the proxy module: 这是代理模块:

const payService = require('../services/pay')
const walletService = require('../services/wallet')

const entity = {
    chargeCard: payService.payByCardToken,
    // ... some other fn
}

module.exports = entity

Based on this example and this response , I tried to stub the required module 'payService': 基于此示例此响应 ,我尝试对所需的模块“ payService”进行存根:

const expect = require('expect.js')
const sinon = require('sinon') 
const entity = require('../entity')
const payService = require('../../services/pay')

describe('Payment entity,', () => {

    it('should proxy functions to each service', () => {

        const stub = sinon.stub(payService, 'payByCardToken')
        entity.chargeCard()
        expect(payService.payByCardToken.called).to.be.ok()

    })
})

But the test fails with: 但是测试失败并显示:

  0 passing (19ms)
  1 failing

  1) Payment entity,
       should proxy functions to each service:
     Error: expected false to be truthy
      at Assertion.assert (node_modules/expect.js/index.js:96:13)
      at Assertion.ok (node_modules/expect.js/index.js:115:10)
      at Function.ok (node_modules/expect.js/index.js:499:17)
      at Context.it (payments/test/entity.js:14:56)

And that's because payService module isn't really stubbed. 那是因为payService模块不是真正的存根。 I know if I add payService as a property of entity and wrap everything with a function, the test will pass: 我知道如果我将payService添加为实体的属性并使用函数包装所有内容,则测试将通过:

// entity
const entity = () => {
    return {
        payService,
        chargeCard: payService.payByCardToken,
        // .. some other fn
    }
}

// test
const stub = sinon.stub(payService, 'payByCardToken')
entity().chargeCard()
expect(payService.payByCardToken.called).to.be.ok()

// test output
Payment entity,
  ✓ should proxy functions to each service

1 passing (8ms)

But that's code added only for testing puposes. 但这只是为了测试目的而添加的代码。 Is there a way to a way to stub module functions without dependency injection and workarounds? 有没有一种方法可以在没有依赖项注入和变通方法的情况下对模块功能进行存根?

The problem is that you're stubbing payService too late, after entity has already set its mappings. 问题是,在entity已设置其映射之后,您将payService存根payService已晚。

If you change your test code like so: 如果您像这样更改测试代码:

const expect = require('expect.js')
const sinon = require('sinon') 
const payService = require('../../services/pay')

describe('Payment entity,', () => {
    let entity

    before(() => {
        sinon.stub(payService, 'payByCardToken')
        entity = require('../entity')
    })

    it('should proxy functions to each service', () => {
        entity.chargeCard()
        expect(payService.payByCardToken.called).to.be.ok()
    })
})

...you should find that entity sets itself up with your stubbed function and the assertion passes okay. ...您应该发现该entity使用存根函数进行了设置,并且断言通过了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM