问题描述:

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.

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.

网友答案:

You need to stringify the responses to test.

expect(scope.depart.toString()).to.deep.equal(new Date().toString());
相关阅读:
Top