You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.4 KiB

  1. /* global jasmine */
  2. 'use strict';
  3. (function(jasmine, beforeEach) {
  4. var spyMatchers = 'called calledOnce calledTwice calledThrice calledBefore calledAfter calledOn alwaysCalledOn calledWith alwaysCalledWith calledWithExactly alwaysCalledWithExactly calledWithMatch alwaysCalledWithMatch'.split(' '),
  5. i = spyMatchers.length,
  6. spyMatcherHash = {},
  7. unusualMatchers = {
  8. "returned": "toHaveReturned",
  9. "alwaysReturned": "toHaveAlwaysReturned",
  10. "threw": "toHaveThrown",
  11. "alwaysThrew": "toHaveAlwaysThrown"
  12. },
  13. getMatcherFunction = function(sinonName, matcherName) {
  14. var original = jasmine.Matchers.prototype[matcherName];
  15. return function () {
  16. if (jasmine.isSpy(this.actual) && original) {
  17. return original.apply(this, arguments);
  18. }
  19. var sinonProperty = this.actual[sinonName];
  20. return (typeof sinonProperty === 'function') ? sinonProperty.apply(this.actual, arguments) : sinonProperty;
  21. };
  22. };
  23. while(i--) {
  24. var sinonName = spyMatchers[i],
  25. matcherName = "toHaveBeen" + sinonName.charAt(0).toUpperCase() + sinonName.slice(1);
  26. spyMatcherHash[matcherName] = getMatcherFunction(sinonName, matcherName);
  27. }
  28. for (var j in unusualMatchers) {
  29. spyMatcherHash[unusualMatchers[j]] = getMatcherFunction(j, unusualMatchers[j]);
  30. }
  31. beforeEach(function() {
  32. this.addMatchers(spyMatcherHash);
  33. });
  34. })(jasmine, beforeEach);