"Create a test when navigating to it" for non-Rails projects

👋, I've recently found out one can create new tests for the currently open file with the command `Navigate | Test`: https://www.jetbrains.com/help/ruby/create-tests.html#navigate

It seems like this only works for Rails projects, as the documentation suggests it runs `rails generate` to create the test file.

The main projects I use RubyMine for are not Rails, but I would love to get this automatic test generation working.

I use RSpec for testing and RubyMine navigates without issue between existing tests and their respective production code with `Navigate | Test`. The `New | Ruby Test` generator is also correctly configured for my project, but using is not as convenient as `Navigate | Test`, given most of the friction comes from navigating to the correct `spec/` directory to create the matching test file.

Given my options, my current workflow is: find a neighbor production file to the file I want to test, execute `Navigate | Test` in that file, create a new test file for the file I wanted to test in the same test directory. This new test file doesn't come with matching information about the file I wanted to test, so I have to set that up myself (e.g. copying the class name and its module path).

Is there a contract my RubyMine Ruby project has to fulfill in order for `Navigate | Test` to suggest me the creation of a test file when one is not present?

I have the liberty to modify my project as I wish (adding arbitrary files, like `bin/rails`, or adding new Rake tasks) if needed.

Thank you!

0

Hello,

Would it be possible to share the content of the Gemfile? 

0

Hi Olga Kuvardina,

My project is open source, so here's the full source code: https://github.com/DataDog/dd-trace-rb.

To remove all ambiguity, let's use this specific commit version https://github.com/DataDog/dd-trace-rb/tree/cd8dc6d2ee88e5c8c737f6d1d8310e2de5f61d47

Let's say you create a new file under `lib`, for example `lib/datadog/new_file.rb`, then use the `Navigate | Test` command in RubyMine, you'll see the "No test subject found" error, but not prompt to create a new test.

Manually creating the respective `spec/datadog/new_file_spec.rb` in the repo allows `Navigate | Test` to works correctly.

The Gemfile is available here: https://github.com/DataDog/dd-trace-rb/blob/cd8dc6d2ee88e5c8c737f6d1d8310e2de5f61d47/Gemfile

Which also loads a few dependencies from the gemspec file here: https://github.com/DataDog/dd-trace-rb/blob/cd8dc6d2ee88e5c8c737f6d1d8310e2de5f61d47/ddtrace.gemspec

Let me know if you need any more information, or can't reproduce the issue.

0

FWIW, I followed marcotc's instructions for https://github.com/DataDog/dd-trace-rb:

  • ran `bundle install`
  • created `lib/datadog/new_file.rb`
  • used `Navigate | Test`

It worked as expected.

RubyMine presented me with a dialog to `Create New Test...`. When I clicked on it, it let me choose which test framework template to create, then created the test file.

Ed

RubyMine 2022.1.2
Build #RM-221.5787.34, built on June 1, 2022
Runtime version: 11.0.15+10-b2043.56 aarch64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
macOS 12.4
GC: G1 Young Generation, G1 Old Generation
Registry:
    debugger.watches.in.variables=false

0

marcotc could you please check whether test folders (spec/test) in your project are marked as test sources?
https://www.jetbrains.com/help/ruby/configuring-project-structure.html#content-root

0

请先登录再写评论。