From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Nic Ferrier Newsgroups: gmane.emacs.devel Subject: Re: Best practice for mocking functions/prompts/etc. Date: Sat, 08 Nov 2014 23:17:50 +0000 Message-ID: <87lhnlmgdd.fsf@ferrier.me.uk> References: <20141108193423.4e021283@forcix> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1415488699 19005 80.91.229.3 (8 Nov 2014 23:18:19 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 8 Nov 2014 23:18:19 +0000 (UTC) Cc: emacs-devel@gnu.org To: Jorgen Schaefer Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sun Nov 09 00:18:12 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 1XnFGl-0001P6-61 for ged-emacs-devel@m.gmane.org; Sun, 09 Nov 2014 00:18:11 +0100 Original-Received: from localhost ([::1]:37073 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XnFGk-0005iC-9N for ged-emacs-devel@m.gmane.org; Sat, 08 Nov 2014 18:18:10 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:50089) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XnFGb-0005gn-Hm for emacs-devel@gnu.org; Sat, 08 Nov 2014 18:18:07 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XnFGV-00017i-4G for emacs-devel@gnu.org; Sat, 08 Nov 2014 18:18:01 -0500 Original-Received: from static.17.66.46.78.clients.your-server.de ([78.46.66.17]:54123 helo=po1.ferrier.me.uk) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XnFGU-00016c-Kj for emacs-devel@gnu.org; Sat, 08 Nov 2014 18:17:55 -0500 Original-Received: from nicferrier-dell-xps (140.35.155.90.in-addr.arpa [90.155.35.140]) by po1.ferrier.me.uk (Postfix) with ESMTPA id D3CEEAC05F2; Sun, 9 Nov 2014 00:36:03 +0100 (CET) Original-Received: from nicferrier-XPS13-9333 (localhost [127.0.0.1]) by nicferrier-dell-xps (Postfix) with ESMTPS id 49DC16BA00; Sat, 8 Nov 2014 23:17:50 +0000 (GMT) In-Reply-To: <20141108193423.4e021283@forcix> (Jorgen Schaefer's message of "Sat, 8 Nov 2014 19:34:23 +0100") X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 78.46.66.17 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:176598 Archived-At: Jorgen Schaefer 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