Lately I’ve been writing a lot of tests. To shorten the round trip times and for some comfort I wanted to run tests in Emacs: I don’t need to switch to the console and I can simply hit enter on an error message to get to the right file at the right position.
While projectile-rails supports running rake tasks, rake can only run the complete test suite or single test cases, not just a single test. This increases the round trip time too much for me, so Emacs to the rescue! I wrote some elisp to run a single rails test:
(defun get-current-test-name () (save-excursion (let ((pos) (test-name)) (re-search-backward "test \"\\([^\"]+\\)\" do") (setq test-name (buffer-substring-no-properties (match-beginning 1) (match-end 1))) (concat "test_" (replace-regexp-in-string " " "_" test-name))))) (defun run-test-at-point () (interactive) (let ((root-dir (projectile-project-root))) (compile (format "ruby -Ilib:test -I%s/test %s -n %s" root-dir (expand-file-name (buffer-file-name)) (get-current-test-name)))))
This little snippet runs the test at point, so the test the cursor is currently located in. I bound it to