unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Nic Ferrier <nferrier@ferrier.me.uk>
To: Jorgen Schaefer <forcer@forcix.cx>
Cc: emacs-devel@gnu.org
Subject: Re: Best practice for mocking functions/prompts/etc.
Date: Sat, 08 Nov 2014 23:17:50 +0000	[thread overview]
Message-ID: <87lhnlmgdd.fsf@ferrier.me.uk> (raw)
In-Reply-To: <20141108193423.4e021283@forcix> (Jorgen Schaefer's message of "Sat, 8 Nov 2014 19:34:23 +0100")

Jorgen Schaefer <forcer@forcix.cx> writes:

> (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?

I don't see any reason to test all those things for every interactive
function. I think interactive working (or not) should be tested once, by
some tests around interactive. You don't have to test that.

In this example, you should just mock read-file-name. Which you're
doing.

Using cl-letf, cl-labels, cl-flet or noflet would all be ok I think.


There are elisp mocking libs. But with lisp you don't really need them.


Nic



  reply	other threads:[~2014-11-08 23:17 UTC|newest]

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=87lhnlmgdd.fsf@ferrier.me.uk \
    --to=nferrier@ferrier.me.uk \
    --cc=emacs-devel@gnu.org \
    --cc=forcer@forcix.cx \
    /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 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).