简体   繁体   English

javascript expect.toBe 具有多个值

[英]javascript expect.toBe with multiple values

I have problem related to daylight saving time.我有与夏令时有关的问题。 I have javascript jasmine test, where I test that opening time is correct.我有 javascript jasmine 测试,我测试开放时间是否正确。 The opening times are stored in GMT-time, because they are gotten from backend api.开放时间以 GMT 时间存储,因为它们是从后端 api 获取的。 The problem is, that the correct opening times cannot be tested with expect.toBe(certain_hour), because now when the daylight-saving went off, the opening hours won't be the same.问题是,不能用 expect.toBe(certain_hour) 测试正确的开放时间,因为现在夏令时结束时,开放时间将不一样。 Maybe it is stupid to try to store opening hours in GMT time anyway, since then the actual opening hour changes.无论如何,尝试将开放时间存储在 GMT 时间可能是愚蠢的,因为那时实际的开放时间会发生变化。 But, how could I test the expect.toBe with multiple values?但是,我怎么能用多个值测试 expect.toBe 呢? For now, I could test that expect.toBe(hour_one || hour_two), but that is not supported?现在,我可以测试 expect.toBe(hour_one || hour_two),但不支持?

The test code is still javascript, so when you write something like this:测试代码仍然是 javascript,所以当你写这样的东西时:

expect.toBe(hour_one || hour_two)

It evaluates hour_one || hour_two它评估hour_one || hour_two hour_one || hour_two as a logical expression. hour_one || hour_two作为逻辑表达式。 In javascript, when an expression of the form a || b在 javascript 中,当一个表达式为a || b a || b is evaluated, the first non-falsy value of the two is returned, so 0 || 5 a || b被评估,返回两者的第一个非假值,所以0 || 5 0 || 5 will return 5, but 2||5 will return 2. Then this evaluation result, as a single argument, is passed to the toBe() function. 0 || 5将返回 5,但2||5将返回 2。然后,此评估结果作为单个参数传递给toBe()函数。

Similar question as Jasmine expect(resultCode).toBe(200 or 409)Jasmine expect(resultCode).toBe(200 or 409)类似的问题

We can use expect().toContain() like this:我们可以像这样使用 expect().toContain():

expect([200,409]).toContain(resultCode);

or not to contain:或不包含:

expect([200,409]).not.toContain(resultCode);

 const isOneOfThemTrue = statusCode === 200 || statusCode === 409; expect(isOneOfThemTrue).toBe(true)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM