I Spy
Description:
NOTE: The test cases for this kata are broken, but for some reason CodeWars has locked them and I cannot edit them. Specifically, the returned
function is not propertly testing that old values are remembered. If and when I can fix the problem, I will, but I don't see any way to do that due to the lock.
In testing, a spy
function is one that keeps track of various metadata regarding its invocations. Some examples of properties that a spy might track include:
- Whether it was invoked
- How many times it was invoked
- What arguments it was called with
- What contexts it was called in
- What values it returned
- Whether it threw an error
For this kata, implement a spyOn
function which takes any function func
as a parameter and returns a spy
for func
. The returned spy
must be callable in the same manner as the original func
, and include the following additional properties/methods:
.callCount()
— returns the number of timesspy
has been called.wasCalledWith(val)
– returnstrue
ifspy
was ever called withval
, else returnsfalse
..returned(val)
— returnstrue
ifspy
ever returnedval
, else returnsfalse
Below is a specific example of how spyOn
might work in the wild.
function adder(n1, n2) { return n1 + n2; }
var adderSpy = spyOn( adder );
adderSpy(2, 4); // returns 6
adderSpy(3, 5); // returns 8
adderSpy.callCount(); // returns 2
adderSpy.wasCalledWith(4); // true
adderSpy.wasCalledWith(0); // false
adderSpy.returned(8); // true
adderSpy.returned(0); // false
Similar Kata:
Stats:
Created | May 12, 2015 |
Published | Jul 3, 2015 |
Warriors Trained | 8891 |
Total Skips | 1238 |
Total Code Submissions | 32114 |
Total Times Completed | 4609 |
JavaScript Completions | 4609 |
Total Stars | 169 |
% of votes with a positive feedback rating | 93% of 342 |
Total "Very Satisfied" Votes | 304 |
Total "Somewhat Satisfied" Votes | 30 |
Total "Not Satisfied" Votes | 8 |
Total Rank Assessments | 17 |
Average Assessed Rank | 5 kyu |
Highest Assessed Rank | 4 kyu |
Lowest Assessed Rank | 6 kyu |