使用茉莉花存根时未定义对象



我对Jasmine很陌生。我打算将其用于香草javascript项目。初始配置轻而易举,但我在使用spyOn时收到对象未定义错误。

我已经下载了 3.4.0 版 Jasmine 发布页面,并将文件"按原样"添加到我的项目中。然后,我相应地更改了jasmine.json文件,并看到所有示例测试都通过了。但是,当尝试spyOn私有对象时,我遇到未定义的错误,

if (typeof (DCA) == 'undefined') {
DCA = {
__namespace: true
};
}
DCA.Audit = {
//this function needs to be tested
callAuditLogAction: function (parameters) {
//Get an error saying D365 is not defined
D365.API.ExecuteAction("bu_AuditReadAccess", parameters,
function (result) { },
function (error) {
if (error != undefined && error.message != undefined) {
D365.Utility.alertDialog('An error occurred while trying to execute the Action. The response from server is:n' + error.message);
}
}
);
}
}

和我的规格类

describe('Audit', function(){
var audit;
beforeEach(function(){
audit = DCA.Audit;
})   
describe('When calling Audit log function', function(){        
beforeEach(function(){
})
it('Should call Execute Action', function(){
var D365 = {
API : {
ExecuteAction : function(){
console.log('called');
}
}
}
// expectation is console log with say hello
spyOn(D365.API, 'ExecuteAction').and.callFake(() => console.log('hello'));
var params = audit.constructActionParameters("logicalName", "someId", 'someId');
audit.callAuditLogAction(params);
})
})
})

如您所见,我的规范类不知道实际D365对象。我希望在不必注入 D365 对象的情况下存根它。我是否需要存根整个 365 库并将其链接到我的测试运行程序 html?

经过一番思考,我让它工作了。因此,包含D365的库仍需要添加到我的测试运行程序 html 文件中。之后我可以伪造方法调用,如下所示,

it('Should call Execute Action', function(){            
spyOn(D365.API, 'ExecuteAction').and.callFake(() => console.log('hello'));
var params = audit.constructActionParameters("logicalName", "someId", 'someId');
audit.callAuditLogAction(params);
})

它现在正在工作。

最新更新