unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Best practice for mocking functions/prompts/etc.
@ 2014-11-08 18:34 Jorgen Schaefer
  2014-11-08 23:17 ` Nic Ferrier
  2014-11-09  0:56 ` Lars Magne Ingebrigtsen
  0 siblings, 2 replies; 6+ messages in thread
From: Jorgen Schaefer @ 2014-11-08 18:34 UTC (permalink / raw)
  To: emacs-devel

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



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2014-11-09 11:05 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-11-08 18:34 Best practice for mocking functions/prompts/etc Jorgen Schaefer
2014-11-08 23:17 ` Nic Ferrier
2014-11-09  8:59   ` Jorgen Schaefer
2014-11-09 10:36     ` Nic Ferrier
2014-11-09 11:05       ` Jorgen Schaefer
2014-11-09  0:56 ` Lars Magne Ingebrigtsen

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).