all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Jorgen Schaefer <forcer@forcix.cx>
To: emacs-devel@gnu.org
Subject: Best practice for mocking functions/prompts/etc.
Date: Sat, 8 Nov 2014 19:34:23 +0100	[thread overview]
Message-ID: <20141108193423.4e021283@forcix> (raw)

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	other threads:[~2014-11-08 18:34 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-11-08 18:34 Jorgen Schaefer [this message]
2014-11-08 23:17 ` Best practice for mocking functions/prompts/etc 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20141108193423.4e021283@forcix \
    --to=forcer@forcix.cx \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.