如何使用笑话测试框架存根类?

时间:2018-10-04 14:30:59

标签: javascript angular typescript unit-testing jestjs

我曾尝试为我的班级创建存根类,但不确定是否这是正确的方法,如果有人可以提供帮助,将不胜感激。

错误:构造函数应该是一个函数

class.ts

export class CacheController {
    public static getInstance(): CacheController {
        if (!CacheController.instance) {
            CacheController.instance = new CacheController();
        }
        return CacheController.instance;
    }

    private static instance: CacheController;
    private cache: CCache;

    private constructor() {
        this.cache = new CCacheFactory().getCachingObject("redis");
    }

    public async saveDetails(reqDetails: IGetCacheRequest): Promise < ICacheResponse > {

    }
}

class.spec.ts

describe("Testing the Cache Controller", () => {
    let stubbedCacheWrapper;
    let mod;
    let result;

    const response = {
        key: "5c70bec10f664215b30538636ed9a6154104a8de5faf4ea39d376fb5010d397c";
    };
    beforeEach(function() {
        mod = CacheController.getInstance();
        stubbedCacheWrapper = sandbox.createStubInstance(CacheController.getInstance());
        stubbedCacheWrapper.getInstance().saveDetails().callsFake(function() {
            // return Promise.resolve(successESLresponse);
            return new Promise < any > ((resolve) => {
                resolve(response);
            });
        });
    });
    describe('setValues() called first time', function() {
        const saveDetails = {}
        as ICacheRequest;
        saveDetails.cacheobject = {
            test: "test data"
        };
        saveDetails.cachetype = "TADCache";
        beforeEach(() => {
            result = mod.saveDetails(saveDetails, stubbedCacheWrapper);
        });
        it("should call call cache.setValue)", () => {
            sinon.assert.calledOnce(stubbedCacheWrapper.saveDetails());
        });
    });

});

0 个答案:

没有答案