5 kyu

I Spy

4,609glebec

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 times spy has been called
  • .wasCalledWith(val) – returns true if spy was ever called with val, else returns false.
  • .returned(val) — returns true if spy ever returned val, else returns false

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
Functional Programming

Similar Kata:

More By Author:

Check out these other kata created by glebec

Stats:

CreatedMay 12, 2015
PublishedJul 3, 2015
Warriors Trained8891
Total Skips1238
Total Code Submissions32114
Total Times Completed4609
JavaScript Completions4609
Total Stars169
% of votes with a positive feedback rating93% of 342
Total "Very Satisfied" Votes304
Total "Somewhat Satisfied" Votes30
Total "Not Satisfied" Votes8
Total Rank Assessments17
Average Assessed Rank
5 kyu
Highest Assessed Rank
4 kyu
Lowest Assessed Rank
6 kyu
Ad
Contributors
  • glebec Avatar
  • joh_pot Avatar
  • Voile Avatar
Ad