Unit Testing

1 - .toBe() or .toEqual()
2 - jasmine.anything()
3 - Test Doubles (spies)
.toBe() or .toEqual()
toBe() for primitive values toEqual() for arrays and objects
expect("Rover").toBe("Rover");
// PASSES
let dog = {name: "Rover"};
expect(dog).toBe({name: "Rover"});
// FAILS
let dog = {name: "Rover"};
expect(dog.name).toBe("Rover");
// PASSING WORK AROUND
let dog = {name: "Rover"};
expect(dog).toEqual({name: "Rover"});
// PASSES=== Deep equality
jamine.anything()
Anything except null or undefined
expect(14).toEqual(jasmine.anything()); // PASSES
expect("hello").toEqual(jasmine.anything()); // PASSESexpect([1,2,3,4]).toContain(4); // PASSES
expect([1,2,3]).toContain(jasmine.anything()); // PASSES
expect([]).toContain(jasmine.anything()); // FAILSexpect(null).toEqual(jasmine.anything()); // FAILS
expect(undefined).toEqual(jasmine.anything()); // FAILSexpect(mySpy).toHaveBeenCalledWith(jasmine.anything());
// PASSES if the 'mySpy' is passed an arg when calledexpect(14).toBe(jasmine.anything()); // FAILSjamine.any(Constructor)
jamine.any(Number)
jamine.any(String)
jamine.any(Boolean)
jamine.any(Array)
jamine.any(Object)
expect(14).toEqual(jasmine.any(Number)); // PASSES
expect(14).toEqual(jasmine.any(String)); // FAILS
expect("hello").toEqual(jasmine.any(String)); // PASSES
expect(false).toEqual(jasmine.any(Boolean)); // PASSES
expect([1,2,3]).toEqual(jasmine.any(Array)); // PASSES
expect({greeting:"Hello"}).toEqual(jasmine.any(Object)); // PASSES
expect(typeof 4 === 'number').toBe(true); // ALSO PASSESjamine.any()
jamine.any(Set)
jamine.any(Regex)
jamine.any(Class)
// 1 - Set
expect(new Set([1,2,3])).toEqual(jasmine.any(Set)); // PASSES
// 2 - Regex
expect(/()/).toEqual(jasmine.any(RegExp)); // PASSES
// 3 - Class
class Greeting {};
let hello = new Greeting();
expect(hello).toEqual(jasmine.any(Greeting)); // PASSESexpect(hello).toBeInstanceOf(Greeting); // PASSESexpect(hello instanceof Greeting).toBe(true); // PASSESjamine.any()
jamine.any(Date)
// 1 - Date
expect(new Date()).toEqual(jasmine.any(Date)); // PASSES
let newMomentDate = moment().toDate();
expect(newMomentDate).toEqual(jasmine.any(Date)); // PASSESjamine.any()
jamine.any(Date)
jamine.any(moment)
// 1 - Date
expect(new Date()).toEqual(jasmine.any(Date)); // PASSES
let newMomentDate = moment().toDate();
expect(newMomentDate).toEqual(jasmine.any(Date)); // PASSES
// 2 - Moment
let newMoment = moment();
expect(newMoment).toEqual(jasmine.any(moment)); // PASSESThings that make testing more difficult
Spies Asynchronous code Dependencies / Mocks Dates and time JS Frameworks / 3rd Party Libraries The DOM
Spies (aka Test Doubles)
spyOn(service, 'greet');
spyOn(service, 'greet').and.stub();
spyOn(service, 'greet').and.returnValue();
spyOn(service, 'greet').and.callFake(); spyOn(service, 'greet').and.callThrough();
What are we actually testing?
Our code from the ticket
Our code in all future tickets
What are tests?
Defense System for future changes
Additional information about your code
Good Opportunity to...ROCK!
Review my code Optimize it Comment it Kick back relax
Unit Testing Part 1, Spy's, jasmine.anything(), toBe() vs toEqual()
By Rich Finelli
Unit Testing Part 1, Spy's, jasmine.anything(), toBe() vs toEqual()
- 137