toBe()
versus : checks equivalence. , on the other hand, makes sure that they're the exact same object.toEqual()
toEqual()
toBe()
I would say use when comparing values, and when comparing objects.toBe()
toEqual()
When comparing primitive types, and will yield the same result. When comparing objects, is a stricter comparison, and if it is not the exact same object in memory this will return false. So unless you want to make sure it's the exact same object in memory, use for comparing objects.toEqual()
toBe()
toBe()
toEqual()
Check this link out for more info : http://evanhahn.com/how-do-i-jasmine/
Now when looking at the difference between and when it comes to numbers, there shouldn't be any difference so long as your comparison is correct. will always be equivalent to .toBe()
toEqual()
5
5
A nice place to play around with this to see different outcomes is here
Update
An easy way to look at and is to understand what exactly they do in JavaScript. According to Jasmine API, found here:toBe()
toEqual()
toEqual() works for simple literals and variables, and should work for objects
toBe() compares with ===
Essentially what that is saying is and are similar Javascripts operator except is also checking to make sure it is the exact same object, in that for the example below as well. However, will return true in that situation.toEqual()
toBe()
===
toBe()
objectOne === objectTwo //returns false
toEqual()
Now, you can at least understand why when given:
var objectOne = {
propertyOne: str,
propertyTwo: num
}
var objectTwo = {
propertyOne: str,
propertyTwo: num
}
expect(objectOne).toBe(objectTwo); //returns false
That is because, as stated in this answer to a different, but similar question, the operator actually means that both operands reference the same object, or in case of value types, have the same value.===