DEV Community

Lucas Barret
Lucas Barret

Posted on • Edited on

How to generate and launch RSpec specs with VsCode (ChatGPT included)

Ruby, Rails, and RSpec are incredibly powerful tools that have greatly facilitated the process of backend development. However, despite the impressive capabilities of Rails and its CLI, there are instances where creating test files from scratch is necessary, such as when dealing with GraphQL queries, or when launching tests through shell scripts.

As a skilled 😎 developer, automation was the obvious solution for me. So, I utilized VSCode tasks to automate the process, and now it works seamlessly

VSCode tasks provide a convenient way to launch processes and run scripts directly from the VSCode palette. Additionally, it is possible to bind keybindings to these tasks for even quicker access.

  1. To set up VSCode tasks for your Ruby project, first create a .vscode file at the root of your project directory. Then, create a .tasks.json file within this directory to define your tasks as follow:

Image description

  1. Then Add these following line to your tasks.json
  "tasks" : [
    {
      "label":"Open/Create test",
      "type": "shell",
      "command": "code spec/\`ruby -e 'puts 
\"${relativeFileDirname}\"
.sub(/app/,\"\")'\`/${fileBasenameNoExtension}_spec.rb",
      "problemMatcher": "$ruby"
    },

    {
      "label": "Run RSpec Test",
      "type": "shell",
      "command": "rspec ${file}"
    }
  ]
Enter fullscreen mode Exit fullscreen mode

The following line can be a little bit cryptic at a first sight :

"code spec/\`ruby -e 
'puts\"${relativeFileDirname}\"
.sub(/app/,\"\")'\`/${fileBasenameNoExtension}_spec.rb"
Enter fullscreen mode Exit fullscreen mode

In essence, this code parses the path of your current file in VSCode, removes the 'app' directory from your Rails project, replaces it with 'spec', and appends 'spec' at the end of the file path. This enables you to maintain a consistent tree structure in both your app and spec directories. Although I chose to implement this in Ruby for fun, it can be written in any programming language of your choice.

  1. And there you go you can now with the palette run your task by: (In the palette) Tasks: Run Task > Open/Create Test for example.

But you can take it a step further and define a keyboard shortcut for your VSCode tasks. To do this, navigate to the palette and select 'Open Keyboard Shortcuts (JSON)'.

Then, add the appropriate keybindings to your keybindings.json file. Note that the 'args' field is crucial for enabling VSCode to find and execute the specified task."

{
        "key": "cmd+shift+r",
        "command": "workbench.action.tasks.runTask",
        "args": "Run RSpec Test"
    },
    {
        "key": "cmd+shift+v",
        "command": "workbench.action.tasks.runTask",
        "args": "Open/Create test"
 }
Enter fullscreen mode Exit fullscreen mode

PS: I tried to improve the code with ChatGPT but it did not do the actual things I wanted.
If you have any feedback or suggestions for improvement, please feel free to leave a comment or suggestion. I'm open to all ideas and would love to hear your thoughts!🙈

Top comments (2)

Collapse
 
antooooony profile image
anto

Great article 👏

Collapse
 
yet_anotherdev profile image
Lucas Barret

Thanks a lot ! 🤓