Apparently I committed to rails before, I’m pretty sure it was a doc update, but none the less this is something I actually like to do more. So first step get a working setup for running rails tests locally and getting a reasonably productive environment setup. Since I’m mostly using emacs these days it would be nice if everything would actually work out of this editor/operating system.
rake test
The easiest way to run the tests after cloning down rails and bundling is to use rake.
$ bundle exec rake test
in the root rails folder will run all the available tests, this is great but when working on something also not what I want most of the time. But running it in a subfolder like activesupport will actually do the right thing and run only this subset of tests
$ cd activesupport $ bundle exec rake test
To make this easier I’m using textmate.el to execute most commands while inside a project from the determined project-root. Meaning if I switch to a file I set the working directory to the current root automatically.
(defun coder/default-project-dir () (if (textmate-project-root) (setq default-directory (textmate-project-root)))) (add-hook 'find-file-hook 'coder/default-project-dir)
Adding .emacs-project files to all the subdirectories will now make sure that when I run a rake command it will be run in the correct directory. Combining this with rake.el allows to run test via a simple M-x rake and selecting test, M-x rake-rerun will now rerun the given tests with a simple command.
This works great while working in quick running test suites but running the whole suite on every change can become inconvenient quickly, this is solved by 2 more enhancements to the flow
running a single test file
Rake allows for passing a file name to limit the run to a single test file
$ bundle exec rake test TEST="test/abstract/callbacks_test.rb"
for example will run only this file of the actionpack testsuite for example. Make sure to run with bundle exec, rake.el does not detect this when in a subfolder, but C-u M-x rake will let you modify the command as needed.
running a single test
Should a file have a lot of tests it can be a good idea to only run a single one, this is possible by passing an option to the minitest testrunner.
$ bundle exec rake test TEST="test/abstract/callbacks_test.rb" TESTOPTS="--name=test_around_action_works"
TESTOPTS will be passed through to minitest which will only run this one test.
integration in emacs
All this works on the command line, as well as using rake.el, personally I use rake.el and have rake rerun bound to C-c r to allow me to quickly run the tests I’m currently working on, this executes in compilation-mode, which allows emacs to parse out file urls and makes them clickable in case of errors.
Next up to make it even better would be to figure out the test name I’m currently in, this works quite simply for files which use the
class TestClass < Minitest::Testcase def test_foo assert_true doing_the_foo end end
syntax, but is much harder with files
class TestClass < Minitest::Testcase test "doing the foo" do assert_true doing_the_foo end end
So for now I stick with doing it by hand.