emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Best practice for mocking functions/prompts/etc.


From: Jorgen Schaefer
Subject: Best practice for mocking functions/prompts/etc.
Date: Sat, 8 Nov 2014 19:34:23 +0100

Hi!
When writing a library for Emacs (to be included in the core), what is
the recommended best practice to test for interactive function calls? I
did not see a mock library, so I suspect there is a standard way without
such a library.

For example, given a description such as "it should prompt the user for
a file", how do I test this the best way?

I came up with this way:


(defun the-function ()
  (read-file-name "Foo: "))


(ert-deftest the-function ()
  ;; Describe the-function

  ;; It should prompt the user for a file name.
  (cl-letf* ((called-prompt nil)
             (test-file "/test-file")
             ((symbol-function 'read-file-name)
              (lambda (prompt)
                (setq called-prompt prompt)
                test-file)))

    (let ((returned-file (the-function)))

      (should (equal returned-file test-file))
      (should (equal called-prompt "Foo: ")))))


Is there a better way? Especially one that makes it easier to check if
the function was called at all and with what arguments, as opposed to
carrying around 1-2 extra variables per mocked function?

Also, is there a standard for the granularity of tests (one test per
feature/description, one test per function, or ...?), and for the
naming of tests?

Regards,
Jorgen



reply via email to

[Prev in Thread] Current Thread [Next in Thread]