[英]Expected '^169.254$' to be /^169.254$/ in jasmine unit testing
I am new to Angular, Karma, Jasmine test framework.我是 Angular、Karma、Jasmine 测试框架的新手。 I am getting the following error.我收到以下错误。 It was working fine.它工作正常。 After upgrade to angular 7, I am getting the below error.升级到 angular 7 后,出现以下错误。
[1A[2K[31mElectron 2.0.2 (Node 8.9.3) HostComponent should call ipAddressPattern and check IP bad IP FAILED[39m Expected '^169.254$' to be /^169.254$/. [1A[2K[31mElectron 2.0.2 (Node 8.9.3) HostComponent 应该调用 ipAddressPattern 并检查 IP 错误 IP FAILED[39m 预期 '^169.254$' 为 /^169.254$/。 at at UserContext.it (karma-test-shim.js:298475:34418) at ZoneDelegate../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (karma-test-shim.js:295054:26) at ProxyZoneSpec../node_modules/zone.js/dist/proxy.js.ProxyZoneSpec.onInvoke (karma-test-shim.js:294539:39) at ZoneDelegate../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (karma-test-shim.js:295053:52) at Zone../node_modules/zone.js/dist/zone.js.Zone.run (karma-test-shim.js:294813:43) at runInTestZone (karma-test-shim.js:294104:34) at UserContext.在 UserContext.it (karma-test-shim.js:298475:34418) 在 ZoneDelegate../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (karma-test-shim.js:295054:26 ) 在 ProxyZoneSpec../node_modules/zone.js/dist/proxy.js.ProxyZoneSpec.onInvoke (karma-test-shim.js:294539:39) 在 ZoneDelegate../node_modules/zone.js/dist/zone.js .ZoneDelegate.invoke (karma-test-shim.js:295053:52) 在 Zone../node_modules/zone.js/dist/zone.js.Zone.run (karma-test-shim.js:294813:43)在 runInTestZone (karma-test-shim.js:294104:34) 在 UserContext。 (karma-test-shim.js:294119:20) at (karma-test-shim.js:294119:20) 在
My code is given below.我的代码如下。
In unit test在单元测试中
it("should call ipAddressPattern and check IP bad IP", () => {
expect(component.ipAddressPattern("169.254.11.11")).toBe(new RegExp(CommonConstants.BAD_IP_ADDRESS_PATTERN));
});
Earlier code was like this expect(component.ipAddressPattern("169.254.11.11")).toBe(CommonConstants.BAD_IP_ADDRESS_PATTERN);
早期的代码是这样的expect(component.ipAddressPattern("169.254.11.11")).toBe(CommonConstants.BAD_IP_ADDRESS_PATTERN);
and it was working.它正在工作。 After upgrade it gave compilation issue.升级后出现编译问题。 So I changed to expect(component.ipAddressPattern("169.254.11.11")).toBe(new RegExp(CommonConstants.BAD_IP_ADDRESS_PATTERN))
In the common constant class, the code is given below.于是我改成expect(component.ipAddressPattern("169.254.11.11")).toBe(new RegExp(CommonConstants.BAD_IP_ADDRESS_PATTERN))
公共常量类中,代码如下。
public static readonly BAD_IP_ADDRESS_PATTERN: string = "^169\.254$";
In other classes, the code is given below.在其他类中,代码如下。
public ipAddressPattern(ipAddress: string): RegExp {
return CommonUtil.isBadIPAddress(ipAddress);
}
In CommonUtil class, the code is given below.在 CommonUtil 类中,代码如下。
public static isBadIPAddress(ipAddress: string): any {
if (ipAddress) {
if (ipAddress.startsWith(CommonConstants.BAD_IP_ADDRESS)) {
return CommonConstants.BAD_IP_ADDRESS_PATTERN;
} else {
return ValidationPatterns.IP_ADDRESS;
}
}
}
Please suggest me how to fix this issue.请建议我如何解决这个问题。
You need to remove the call to new RegExp()
.您需要删除对new RegExp()
的调用。 That's going to return a new RegExp
object each time, causing the test to fail.每次都会返回一个新的RegExp
对象,导致测试失败。
To fix the compilation error, you need to correct the return type of ipAddressPattern
.要修复编译错误,您需要更正ipAddressPattern
的返回类型。 You're not returning a RegExp
object, you're returning a string
.您不是返回RegExp
对象,而是返回string
。
So the signature needs to be: public ipAddressPattern(ipAddress: string): string
所以签名需要是: public ipAddressPattern(ipAddress: string): string
在JavaScript中, String.prototype.startsWith
方法(@line 3在CommonUtil
类)期待一个原始字符串,而不是一个正则表达式(字符串格式)。
expect(component.ipAddressPattern("169.254.11.11").toString()).toBe(new RegExp(CommonConstants.BAD_IP_ADDRESS_PATTERN).toString().replace(/^\\/|\\/$/g, ''))
When I added "@types/jasmine": "^2.8.7",
, in devDependencies, the problem got solved.当我在 devDependencies 中添加"@types/jasmine": "^2.8.7",
,问题就解决了。 So I did like this.所以我喜欢这个。
"devDependencies": {
"@angular/compiler-cli": "^7.2.0",
"@angular/language-service": "~7.2.0",
"@types/jasminewd2": "~2.0.3",
"@types/core-js": "^0.9.46",
"@types/jasmine": "^2.8.7",
... other modules ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.