0

我存根getElementByIdbeforeEach并希望在另一个测试之前恢复它,并再次存根返回另一个值。因为现在我收到错误 TypeError: Attempted to wrap getElementById which is already wrapped

 let loginUrl = 'loginUrl'
    const url = '/app/auth'
    const textContent = `{"${loginUrl}":"${url}"}`
    let htmlDecode

    describe('identityServer', () => {

        beforeEach(() => {
            htmlDecode = sinon.stub().returns(textContent)
            sinon.stub(document, 'getElementById').returns({textContent})
            sinon.stub(htmlEncoder, 'Encoder').returns({htmlDecode: () => htmlDecode})

            identityServerModel()
        })

        it('should return correct model for IdentityServer', () => {
            window.identityServer.getModel().should.deep.equal({[loginUrl]: url})
        })
    })

    describe('identityServer', () => {

        beforeEach(() => {
            htmlDecode = sinon.stub().returns(textContent)
            sinon.stub(document, 'getElementById').returns({innerHTML: textContent})
            sinon.stub(htmlEncoder, 'Encoder').returns({htmlDecode: () => htmlDecode})

            identityServerModel()
        })
        it('should return correct model using serialization HTML from innerHTML property when textContent is undefined', () => {
            window.identityServer.getModel().should.deep.equal({[loginUrl]: url})
        })
    })
4

1 回答 1

0

尝试添加:

afterEach(() => {
    document.getElementById.restore();
})

进入每个describe(...).

于 2018-07-01T19:07:11.737 回答