Jasmine间谍对象依赖于另一个对象,注入mock

时间:2017-02-08 03:56:24

标签: typescript jasmine

我在Security.ts类中有以下TypeScript构造函数

constructor(readonly idOptions: IdentityOptions) {
    console.log('constructor called');
    this.lock = new Auth0Lock(idOptions.clientId, idOptions.domain, {
        auth: {
            redirectUrl: idOptions.redirectUrl,
            responseType: 'code',
            params: {
                scope: 'openid'
            }
        },
        initialScreen: idOptions.initialScreen,
        allowedConnections: ['Username-Password-Authentication']
    });
}

现在在Security.ts的单元测试中(使用Jasmine)我想完全模拟Auth0Lock对象,而不是调用真正的Auth0Lock对象。我怎么能用茉莉花间谍来做这件事?

1 个答案:

答案 0 :(得分:0)

创建安全性类的实例后,您可以将Auth0Lock模拟对象分配给实例lock属性。像这样:

let securityObject = new SecurityClass();
let mockedAuth0Lock = {
    clientId1: 22,
    someMethod: jasmine.createSpy('Auth0LockMethod')
};
securityObject.lock = mockedAuth0Lock;