使用Sinon的存根来测试对象上的一个方法。

huangapple go评论162阅读模式
英文:

using sinon stub to test a method on an object

问题

我想在对象内部测试一个函数。 代码,我们将其称为someFile.js,结构如下。

  1. import someService from 'a-location';
  2. const val = someService.getVal();
  3. const AnObject = {
  4. someMethod() {
  5. if (val) //dosomething
  6. }
  7. }
  8. export default AnObject;

在测试文件中,我有以下代码

  1. import someService from 'a-location';
  2. import AnObject from 'someFile';
  3. describe("description", function () {
  4. it('more description', function () {
  5. sinon.stub(someService, 'getVal').returns('my-val');
  6. AnObject.someMethod();
  7. })
  8. })

我希望在someFile.js中调用someService.getVal()时接收'my-val',但它并没有按预期工作。 我做错了什么?

英文:

I want to test a function inside an object. The code, lets call it someFile.js, is structured as below.

  1. import someService from 'a-location';
  2. const val = someService.getVal();
  3. const AnObject = {
  4. someMethod() {
  5. if(val) //dosomething
  6. }
  7. }
  8. export default AnObject;

In the test file, I have code as below

  1. import someService from 'a-location';
  2. import AnObject from 'someFile';
  3. describe("description", function(){
  4. it('more description', function() {
  5. sinon.stub(someService, 'getVal').returns('my-val');
  6. AnObject.someMethod();
  7. })
  8. })

I was hoping to receive 'my-val' when someService.getVal() is called in the someFile.js but it isn't working as expected. What am I doing wrong?

答案1

得分: 0

你应该在存根化后使用import()来动态导入someFile中的AnObject

例如:

some-file.ts:

  1. import someService from './a-location';
  2. const val = someService.getVal();
  3. const AnObject = {
  4. someMethod() {
  5. console.log('val: ', val);
  6. },
  7. };
  8. export default AnObject;

a-location.ts:

  1. const someService = {
  2. getVal() {
  3. return 'real val';
  4. },
  5. };
  6. export default someService;

some-file.test.ts:

  1. import sinon from 'sinon';
  2. import someService from './a-location';
  3. describe('description', () => {
  4. it('more description', async () => {
  5. sinon.stub(someService, 'getVal').returns('my-val');
  6. const AnObject = (await import('./some-file')).default;
  7. AnObject.someMethod();
  8. });
  9. });

测试结果:

  1. description
  2. val: my-val
  3. more description
  4. 1 passing (8ms)

正如你所见,valmy-val,这是一个存根化的值。

英文:

You should use import() to import the AnObject from someFile dynamically after stubbing.

E.g.

some-file.ts:

  1. import someService from './a-location';
  2. const val = someService.getVal();
  3. const AnObject = {
  4. someMethod() {
  5. console.log('val: ', val);
  6. },
  7. };
  8. export default AnObject;

a-location.ts:

  1. const someService = {
  2. getVal() {
  3. return 'real val';
  4. },
  5. };
  6. export default someService;

some-file.test.ts:

  1. import sinon from 'sinon';
  2. import someService from './a-location';
  3. describe('description', () => {
  4. it('more description', async () => {
  5. sinon.stub(someService, 'getVal').returns('my-val');
  6. const AnObject = (await import('./some-file')).default;
  7. AnObject.someMethod();
  8. });
  9. });

Test result:

  1. description
  2. val: my-val
  3. more description
  4. 1 passing (8ms)

As you can see, the val is my-val which is a stubbed value.

huangapple
  • 本文由 发表于 2023年3月15日 18:04:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75743183.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定