Sinon:"尝试包装已经包装的createMessage"

时间:2016-07-20 00:12:40

标签: javascript testing sinon

首先,我已经浏览了与此主题相关的所有其他帖子并实施了Sinon.sandbox()。不幸的是,我仍然收到以下错误:

TypeError: Attempted to wrap createMessage which is already wrapped

请注意,我使用Ava进行测试,在这种情况下,我已将其设置为以串行模式而非并行模式运行。

// Load modules

import Queue from '../lib/queue';

import test from 'ava';
import Sinon from 'sinon';
import Proxyquire from 'proxyquire';

// Globals

const FAKE_APPOINTMENT_ID = 'ab9e9495-fdbf-4607-8f57-01c6e91bd8f5';

let push;
let queueStub;
let sandbox;


// Test setup

test.beforeEach(() => {

    // create the sinon sandbox
    sandbox = Sinon.sandbox.create();

    // stub the queue module
    queueStub = sandbox.stub(Queue.prototype);

    // inject the stub
    push = Proxyquire('../lib/push', {
        './queue': queueStub
    });
});


test.afterEach(t => {

    sandbox.restore();
});

// Tests

test.only('sends an appointment approved push notification', async t => {

    await push.appointmentApproved(FAKE_APPOINTMENT_ID);

    t.true(queueStub.createMessage.calledOnce);
});


test.only('sends an appointment cancelled push notification', async t => {

    await push.appointmentCancelled(FAKE_APPOINTMENT_ID);

    t.true(queueStub.createMessage.calledOnce);
});


test.only('sends an appointment updated push notification', async t => {

    await push.appointmentUpdated(FAKE_APPOINTMENT_ID);

    t.true(queueStub.createMessage.calledOnce);
});

我也尝试将beforeEach切换到before,当3次测试中的第一次通过时,最后两次测试完成,因为存根的调用计数似乎永远不会重置。也许我误解了restore()

的目的

谢谢!

0 个答案:

没有答案