Winnemucca
Winnemucca

Reputation: 3458

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.

Upvotes: 1

Views: 744

Answers (2)

phuzi
phuzi

Reputation: 13060

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.

Upvotes: 1

Winnemucca
Winnemucca

Reputation: 3458

You need to stringify the responses to test.

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

Upvotes: 0

Related Questions