简体   繁体   English

通过日期测试角度单元测试

[英]Pass date testing angular unit tests

I have a ternary that seems to be returning a passing test, but it is failing.我有一个三元似乎正在返回通过测试,但它失败了。

$scope.depart = (typeof serverShortDate !== 'undefined') ? new Date(serverShortDate) : new Date();



AssertionError: expected Wed, 30 Mar 2016 21:26:12 GMT to deeply equal Wed, 30 Mar 2016 21:26:12 GMT

Here is my simple spec这是我的简单规格

    expect(scope.depart).to.deep.equal(new Date());

All that I can imagine is that somewhere is a difference.我所能想象的只是某处有所不同。 The error message shows the same values.错误消息显示相同的值。

The problem you have that === on objects checks that the two objects are the same object.您在对象上的===问题会检查两个对象是否是同一个对象。 === works slightly differently for objects when compared to strings or numbers. ===与字符串或数字相比,对象的工作方式略有不同。

Your test appears compares a new Date object to scope.depart , by definition these objects are not the same object and can never be 'deeply' equal.您的测试似乎将一个新的 Date 对象与scope.depart进行比较,根据定义,这些对象不是同一个对象,永远不可能“完全”相等。

You could change your test to:您可以将测试更改为:

expect(scope.depart.valueOf()).to.deep.equal((new Date()).valueOf());

to check that both dates represent the same date/time.检查两个日期是否代表相同的日期/时间。

您需要对要测试的响应进行字符串化。

expect(scope.depart.toString()).to.deep.equal(new Date().toString());

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

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