0

I have written a Scala class in my project and wish to stub the corresponding src/test/**/TestClass.scala by some magical key command or equivalent. Is there a way to do this that is hidden within VS Code or Metals?

edit: I was too general here. I'm not looking for stubbing out tests within a test class for some existing class. I'm looking for feature parity with JetBrains, where I can right-click on a class and get the option to create a corresponding test file within the project. The generated test file contains by default a package line, and an empty class definition that extents a unit testing framework API such as scalatest. The test file template can be further configured by the user via a preferences menu.

  • what is a `magical key command` – rioV8 Apr 27 '23 at 15:48
  • no command is hidden in VSC, just use the key bind dialog and type `scala` – rioV8 Apr 27 '23 at 15:49
  • @rioV8 it's a cute term to cover things such as key bindings provided by VS Code, key bindings tied to features provided by Metals, and perhaps also features that are not by default tied to any particular key. – Rob Fitzgerald Apr 27 '23 at 17:19
  • let me add that i have explored the key bindings dialog and not found something for this. – Rob Fitzgerald Apr 27 '23 at 17:22
  • There is no such thing. You can start writing `new TestClass` and IDE might fill in the missing implementations with `???`. There might be some mocking framework in project and then you use `val stub = mock[TestClass]`. I have not heard of language that have its IDE generate mock code specifically. Maybe try Copilot or ChatGPT? – Mateusz Kubuszok Apr 27 '23 at 17:51
  • all commands are to be found in the key bind dialog not only the once with one or more key bindings – rioV8 Apr 27 '23 at 18:17
  • most of the time people write useless trivial tests, if those tests would fail the application would not work in the first place, tests are there for non trivial/edge cases and it is very hard to get these generated automatic, or you need to elaborate more on what you mean with _stub the corresponding test class_ – rioV8 Apr 27 '23 at 18:21
  • maybe search the marketplace if somebody has already written such a feature, and if not found be the first to write such an extension, I assume you have the assumption that what you want is possible to automate, if so you can write a piece of JS to do that. Be aware that it might take you more than 1 hour to write – rioV8 Apr 27 '23 at 18:24
  • Thanks for the feedback @MateuszKubuszok. I've mislead you, so, I clarified my question. Just looking for the fairly simple test-from-template generation feature that exists in JetBrains. – Rob Fitzgerald Apr 27 '23 at 18:29
  • @rioV8 definitely headed to the marketplace next, assuming Mateusz's comment is correct. – Rob Fitzgerald Apr 27 '23 at 18:30
  • with [File Templates](https://marketplace.visualstudio.com/items?itemName=rioj7.vscode-file-templates) you can create a template and use variables and commands to fill in the gaps – rioV8 Apr 27 '23 at 18:36
  • @rioV8 that's a perfect next step, thanks for sharing (and, if you're rioj7, thanks for authoring a generic solution!). building a file template that understands java-style project directory structures would be a nice time saver. – Rob Fitzgerald Apr 27 '23 at 19:35
  • everybody uses a different project structure, if you can't find a command to get some info from your project create an issue, the extension `command-variable` contains a lot of commands you could use, also have a look at `my-code-action` to create quick fixes that generate text in the same file or other files based on the text under the cursor – rioV8 Apr 27 '23 at 20:13

0 Answers0