DEV Community

CHADDA Chakib
CHADDA Chakib

Posted on

A little of ELISP to improve your RAILS testing

Why spacemacs

I am a big big fan of vim, but I struggled with it when I was coding with multiple project in the same time.
The big pain was also when I need to see the diffs before committing.

So I moved to spacemacs (vim mode) several years ago and I am loving it.
I still use vim to edit some config files but the main code in on spacemacs.

Testing rails inside spacemacs sucks

Now, I am coding a lot of ruby mainly for rails projects and testing function included in the official ruby layer are not adapted for a modern rails test with minitest.

With rails now, when we call bundle exec rails test the code will try to find the spring process and call the tests inside.
The spring process improve a lot the boot-up time of ruby and the loading of all your rails project by caching it.

In spacemacs, your cursor is within test method and you hit space m t t, this will run only this test but he is calling your test by using ruby ..... [your test file] and this will not leverage all the optimization done in the rake test command shipped in rails.

More dangerous, this will flush your dev database and populate it with your test fixture.

bad bad .... very bad thing.

What I was doing before the help of elips

I have a terminal somewhere open, and when I need to run test, I switch to it and try to type the name of the test and line number of the test.

bundle exec rails test test/system/ ... / ... / ... / my_test_file_test.rb:123
Enter fullscreen mode Exit fullscreen mode

Elisp, please help me !

I have written a small functions in Elisp because I want learn & practice this powerful language.

Here is the code, can be improved but who cares, it's doing the job :D

  (defun run-rails-test-file ()
    (interactive)
    (let ((filename (ruby-test-find-file)))
      (compile (format "bundle exec rails test %s" filename))))

  (defun run-rails-test-at-point ()
    (interactive)
    (let* ((filename (ruby-test-find-file))
           (buffername (get-file-buffer filename)))
      (with-current-buffer buffername
        (let ((line (line-number-at-pos (point))))
          (compile (format "bundle exec rails test %s:%s" filename line))))
      )
    )

  (spacemacs/set-leader-keys "mtt" 'run-rails-test-at-point)
  (spacemacs/set-leader-keys "mtT" 'run-rails-test-file)


Enter fullscreen mode Exit fullscreen mode

Let's explain this code

(interactive): make your function callable with space space
emacs call function

ruby-test-find-file: this function will output the path of the current buffer if it's a test file and .... wait for it .... it will also return the last visited test file if you are in a none-test file.

This is very useful, imagine your current buffer is a test file, you run the test, it fails, so you switch to the actual file containing the code, you tweak it and you want to re-run the test, no need to switch again to the test file, this function will handle it for you.

I am reusing the functions defined in the spacemacs ruby layer.

Understand the code by yourself

In Emacs, Everything is a function, and you can read the actual code of all of them.
In spacemacs, the BEST helper is Space h d f and it will autocomplete the name of your function and show you the doc/code.

Let's try to understand what means with-current-buffer

type SPACE h d f and start type the name of the function.

search for function

Oh look to this this cutie, hit enter

the doc of the function
Everything is documented !

Let's go to the code now, move your cursor on the filename and hit Enter
the code

And we can recursively search and learn more things at each key's hit.

Hope you have learned something new and maybe this will help you with your rails workflow on this incredible piece of software Emacs !

Top comments (0)