TypeError: undefined is not a constructor in jasmine unit test with angularjs
TypeError: undefined is not a constructor in jasmine unit test with angularjs
我在 Jesmine 测试中遇到上述错误。代码有一个控制器,它调用 AppsService 服务来接收应用程序数组的承诺响应,然后是 onLoad 方法 -
AppsService.initAndGetApps(appCredentialType)
.then(onLoad)
.catch(onError);
代码工作正常,但单元测试出现错误,如 -
TypeError: undefined is not a constructor (evaluating 'AppsService.initAndGetApps(appCredentialType).then(onLoad)')
我尝试在我的规范文件中使用 Jasmine 模拟 initAndGetApps 以及像这样的自定义方法(两者都给出相同的上述错误)-
1.
initAndGetApps : jasmine.createSpy("initAndGetApps").and.callFake(function (credentialType) {
return []; // empty is ok
}
2。
AppsService = {
initAndGetApps: function (credentialType) {
return [];
}
}
$provide.value('AppsService', AppsService);
AppsService 在根据 credentialType 参数进行一些计算后使用 deferred.promise 到 return 承诺响应(它还执行两次 Promise 调用)。测试无法调用 initAndGetApps,因为未在 initAndGetApps 的第一行获取 credentialType 的控制台。
谁能指导我模拟 AppsService 的正确方法。
您没有return在这两个模拟中none 做出承诺。如果沿着这些方向进行操作,我会尝试:
initAndGetApps : jasmine.createSpy("initAndGetApps").and.returnValue(
new Promise(function(resolve, reject) {
// not taking our time to do the job
resolve(desiredReturnValue);
}
);
您可以将 return 值更改为模拟值,以便您的代码正常工作。
我在 Jesmine 测试中遇到上述错误。代码有一个控制器,它调用 AppsService 服务来接收应用程序数组的承诺响应,然后是 onLoad 方法 -
AppsService.initAndGetApps(appCredentialType)
.then(onLoad)
.catch(onError);
代码工作正常,但单元测试出现错误,如 -
TypeError: undefined is not a constructor (evaluating 'AppsService.initAndGetApps(appCredentialType).then(onLoad)')
我尝试在我的规范文件中使用 Jasmine 模拟 initAndGetApps 以及像这样的自定义方法(两者都给出相同的上述错误)-
1.
initAndGetApps : jasmine.createSpy("initAndGetApps").and.callFake(function (credentialType) {
return []; // empty is ok
}
2。
AppsService = {
initAndGetApps: function (credentialType) {
return [];
}
}
$provide.value('AppsService', AppsService);
AppsService 在根据 credentialType 参数进行一些计算后使用 deferred.promise 到 return 承诺响应(它还执行两次 Promise 调用)。测试无法调用 initAndGetApps,因为未在 initAndGetApps 的第一行获取 credentialType 的控制台。
谁能指导我模拟 AppsService 的正确方法。
您没有return在这两个模拟中none 做出承诺。如果沿着这些方向进行操作,我会尝试:
initAndGetApps : jasmine.createSpy("initAndGetApps").and.returnValue(
new Promise(function(resolve, reject) {
// not taking our time to do the job
resolve(desiredReturnValue);
}
);
您可以将 return 值更改为模拟值,以便您的代码正常工作。