Unit Testing

Unit testing is when you write a test for each unit (or each function/method) in a program. Each unit test is made up of assertion statements.

assertEqual: This can only be used to check scalar values (numbers or strings), not objects or arrays.

Screen Shot 2017-03-15 at 11.44.50 AM

assertWithinRange: This checks if output is within a certain range.

Screen Shot 2017-03-15 at 11.46.12 AM

assertArraysEqual:  To assert that two arrays are equal, you must first check that they’re both equal in values and lengths.

Screen Shot 2017-03-15 at 11.41.14 AM

assertObjectsEqual: objects must be converted to strings first to be compared.

Screen Shot 2017-03-15 at 11.45.51 AM

How to call an assertion:

assertEqual(findLongestPalindrome('racecar hannah'), 'racecar', 'finds longest of two palindromes');
Advertisements
Unit Testing

Exercism JS: Hello World

Write a function that greets the user by name, or by saying “Hello, World!” if no name is given.

My solutions: 

screen-shot-2016-12-09-at-1-30-21-pm

Note to self:  Don’t forget ’em brackets, semi-colons, and ‘===’.

Test Cases:

screen-shot-2016-12-09-at-1-30-56-pm

I was going to skip this problem since I solved it in Ruby already, but good thing I decided not to because JS syntax is a totally different & confusing beast.

More implementations to mull over here.

Also, here’s how to run exercism tests with jasmine-node:

Change ‘xit’ to ‘it’ when you’re ready to run a test case.

Execute the test file with the following code:

cd ~/exercism/javascript/bob
jasmine-node bob_test.spec.js
Exercism JS: Hello World