Skip to main content
edited tags
Link
Drew
  • 79.1k
  • 10
  • 123
  • 257
replaced http://emacs.stackexchange.com/ with https://emacs.stackexchange.com/
Source Link

I'd like to patch some elisp functions inside my unit tests. I've tried cl-labels and cl-flet, but neither achieve the result I want:

(defun return-number ()
  1)

(defun calls-return-number ()
  (return-number))

(defun patch-cl-labels ()
  (cl-labels ((return-number () 2))
    (calls-return-number)))

(message "After patching: %s" (patch-cl-labels))

I've looked at monkey-patching a functionmonkey-patching a function (but this permanently changes behaviour) and patching a third-party functionpatching a third-party function (this uses advice and holds on to the old function).

I'd like to patch some elisp functions inside my unit tests. I've tried cl-labels and cl-flet, but neither achieve the result I want:

(defun return-number ()
  1)

(defun calls-return-number ()
  (return-number))

(defun patch-cl-labels ()
  (cl-labels ((return-number () 2))
    (calls-return-number)))

(message "After patching: %s" (patch-cl-labels))

I've looked at monkey-patching a function (but this permanently changes behaviour) and patching a third-party function (this uses advice and holds on to the old function).

I'd like to patch some elisp functions inside my unit tests. I've tried cl-labels and cl-flet, but neither achieve the result I want:

(defun return-number ()
  1)

(defun calls-return-number ()
  (return-number))

(defun patch-cl-labels ()
  (cl-labels ((return-number () 2))
    (calls-return-number)))

(message "After patching: %s" (patch-cl-labels))

I've looked at monkey-patching a function (but this permanently changes behaviour) and patching a third-party function (this uses advice and holds on to the old function).

Source Link
Wilfred Hughes
  • 7k
  • 2
  • 33
  • 60

How do I temporarily mock a function for testing?

I'd like to patch some elisp functions inside my unit tests. I've tried cl-labels and cl-flet, but neither achieve the result I want:

(defun return-number ()
  1)

(defun calls-return-number ()
  (return-number))

(defun patch-cl-labels ()
  (cl-labels ((return-number () 2))
    (calls-return-number)))

(message "After patching: %s" (patch-cl-labels))

I've looked at monkey-patching a function (but this permanently changes behaviour) and patching a third-party function (this uses advice and holds on to the old function).