无法与Sinon固执

发布于 2025-01-29 05:01:00 字数 2088 浏览 2 评论 0原文

我需要测试以下createfacebookAdvideOfroMurl(),它消耗了我想与Sinon固执的重取代> Retryasynccall

async function createFacebookAdVideoFromUrl(accountId, videoUrl, title, facebookToken = FACEBOOK_TOKEN, options = null, businessId = null) {
  const method = 'POST';
  const url = `${FACEBOOK_URL}${adsSdk.FacebookAdsApi.VERSION}/${accountId}/advideos`;

  const formData = {
    access_token: businessId ? getFacebookConfig(businessId).token : facebookToken,
    title,
    name: title,
    file_url: videoUrl,
  };

  const callback = () => requestPromise({ method, url, formData });

  const name = 'createFacebookAdVideoFromUrl';
  const retryCallParameters = buildRetryCallParameters(name, options);

  const adVideo = await retryAsyncCall(callback, retryCallParameters);

  logger.info('ADVIDEO', adVideo);

  return { id: JSON.parse(adVideo).id, title };
}

retrysyasynccall函数被导出为这样的:

module.exports.retryAsyncCall = async (callback, retryCallParameters, noRetryFor = [], customRetryCondition = null) => {
 // Implementation details ...
}

这是我到目前为止写过测试的方式:

it.only("should create the video calling business's Facebook ids", async () => {
        const payload = createPayloadDataBuilder({
          businessId: faker.internet.url(),
        });

        const retryAsyncCallStub = sinon.stub(retryAsyncCallModule, 'retryAsyncCall').resolves('random');

        const createdFacebookAd = await FacebookGateway.createFacebookAdVideoFromUrl(
          payload.accountId,
          payload.videoUrl,
          payload.title,
          payload.facebookToken,
          payload.options,
          payload.businessId,
        );

        assert.strictEqual(retryAsyncCallStub.calledOnce, true);
        assert.strictEqual(createdFacebookAd, { id: 'asdf', title: 'asdf' });
      });

我不希望它以TDD方式工作,但我确实希望renryasynccall会被固定。但是,我仍然拥有此typeError:无法读取Mocha中未定义的错误的属性'Inc',该错误是指renryaSynccall的内部函数。

我该如何使Sinon固执工作?

I need to test the following createFacebookAdVideoFromUrl() that consumes a retryAsyncCall that I'd like to stub with Sinon :

async function createFacebookAdVideoFromUrl(accountId, videoUrl, title, facebookToken = FACEBOOK_TOKEN, options = null, businessId = null) {
  const method = 'POST';
  const url = `${FACEBOOK_URL}${adsSdk.FacebookAdsApi.VERSION}/${accountId}/advideos`;

  const formData = {
    access_token: businessId ? getFacebookConfig(businessId).token : facebookToken,
    title,
    name: title,
    file_url: videoUrl,
  };

  const callback = () => requestPromise({ method, url, formData });

  const name = 'createFacebookAdVideoFromUrl';
  const retryCallParameters = buildRetryCallParameters(name, options);

  const adVideo = await retryAsyncCall(callback, retryCallParameters);

  logger.info('ADVIDEO', adVideo);

  return { id: JSON.parse(adVideo).id, title };
}

This retryAsyncCall function is exported as such:

module.exports.retryAsyncCall = async (callback, retryCallParameters, noRetryFor = [], customRetryCondition = null) => {
 // Implementation details ...
}

Here is how I wrote my test so far:

it.only("should create the video calling business's Facebook ids", async () => {
        const payload = createPayloadDataBuilder({
          businessId: faker.internet.url(),
        });

        const retryAsyncCallStub = sinon.stub(retryAsyncCallModule, 'retryAsyncCall').resolves('random');

        const createdFacebookAd = await FacebookGateway.createFacebookAdVideoFromUrl(
          payload.accountId,
          payload.videoUrl,
          payload.title,
          payload.facebookToken,
          payload.options,
          payload.businessId,
        );

        assert.strictEqual(retryAsyncCallStub.calledOnce, true);
        assert.strictEqual(createdFacebookAd, { id: 'asdf', title: 'asdf' });
      });

I don't expect it to work straightaway as I am working in TDD fashion, but I do expect the retryAsyncCall to be stubbed out. Yet, I am still having this TypeError: Cannot read property 'inc' of undefined error from mocha, which refers to an inner function of retryAsyncCall.

How can I make sinon stubbing work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烂柯人 2025-02-05 05:01:00

我通过更改在我的SUT中导入的方式来修复它:

// from 
const { retryAsyncCall } = require('../../../helpers/retry-async');
// to
const retry = require('../../../helpers/retry-async');

在我的测试文件中:

// from 
import * as retryAsyncCallModule from '../../../src/common/helpers/retry-async';
// to
import retryAsyncCallModule from '../../../src/common/helpers/retry-async';

破坏性的使用似乎是副本而不是使用相同的参考,因此,该存根在正确的参考文献上没有应用。

I fixed it by changing the way to import in my SUT :

// from 
const { retryAsyncCall } = require('../../../helpers/retry-async');
// to
const retry = require('../../../helpers/retry-async');

and in my test file :

// from 
import * as retryAsyncCallModule from '../../../src/common/helpers/retry-async';
// to
import retryAsyncCallModule from '../../../src/common/helpers/retry-async';

The use of destructuring seemed to make a copy instead of using the same reference, thus, the stub was not applied on the right reference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文