Setup VS Code to run rails test
Either you use RSpec or Mini test you want an easy way to run the test file you are working on, or you might like to run the test under cursor to quickly validate if the test are passing. Most of the time we run rails test
command manually in the terminal and its time consuming. For vim users there are so many great vim plugins like vim-test.
In this article I am going to show you how you can easily setup a keybinding without using any additional VS code plugins to run tests directly from your editor.
Open a visual studio code.
Click on setting -> Keyboard shortcuts (you can also use
ctrl/cmd K, ctrl/cmd S
Open the setting file by clicking on the file icon at top right corner
And add following 2 shortcuts, you can change the shortcut keys as you like, or add more if you want to run all the test cases
{ // run all tests in current test file "key": "shift+cmd+r", "command": "workbench.action.terminal.sendSequence", "args": { "text": "bin/rails test ${relativeFile}\u000D" } }, { // Run test at current line "key": "shift+cmd+i", "command": "workbench.action.terminal.sendSequence", "args": { "text": "bin/rails test ${relativeFile}:${lineNumber}\u000D" } }
Open your terminal
ctrl/cmd ~
and also open your test fileYou can use these shortcuts
shift + cmd/ctrl + r
to run all tests under current fileOr use
shift + cmd/ctrl + i
to run specific test under cursor.
Enjoy!!