From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Jorgen Schaefer Newsgroups: gmane.emacs.devel Subject: Best practice for mocking functions/prompts/etc. Date: Sat, 8 Nov 2014 19:34:23 +0100 Message-ID: <20141108193423.4e021283@forcix> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1415471706 28159 80.91.229.3 (8 Nov 2014 18:35:06 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 8 Nov 2014 18:35:06 +0000 (UTC) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sat Nov 08 19:34:59 2014 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1XnAqe-00063r-Qg for ged-emacs-devel@m.gmane.org; Sat, 08 Nov 2014 19:34:56 +0100 Original-Received: from localhost ([::1]:36585 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XnAqe-00027n-Ca for ged-emacs-devel@m.gmane.org; Sat, 08 Nov 2014 13:34:56 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:42735) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XnAqK-00027e-7s for emacs-devel@gnu.org; Sat, 08 Nov 2014 13:34:43 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XnAqC-0006cV-IK for emacs-devel@gnu.org; Sat, 08 Nov 2014 13:34:36 -0500 Original-Received: from loki.jorgenschaefer.de ([87.230.15.51]:47741) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XnAqC-0006cO-CE for emacs-devel@gnu.org; Sat, 08 Nov 2014 13:34:28 -0500 Original-Received: by loki.jorgenschaefer.de (Postfix, from userid 998) id 7CCB3202F77; Sat, 8 Nov 2014 19:34:25 +0100 (CET) Original-Received: from forcix (port-21132.pppoe.wtnet.de [46.59.144.53]) by loki.jorgenschaefer.de (Postfix) with ESMTPSA id 4BAB4202F75 for ; Sat, 8 Nov 2014 19:34:25 +0100 (CET) X-Mailer: Claws Mail 3.11.1 (GTK+ 2.24.25; i586-pc-linux-gnu) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 87.230.15.51 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:176580 Archived-At: 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