[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Best practice for mocking functions/prompts/etc.
From: |
Nic Ferrier |
Subject: |
Re: Best practice for mocking functions/prompts/etc. |
Date: |
Sat, 08 Nov 2014 23:17:50 +0000 |
Jorgen Schaefer <address@hidden> 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