Tests for New File Action
How do I test a New File action in the NewGroup, including bringing up the Dialog, selecting the the language, checking the validation and then checking that the file is created in the correct directory? Specifically, New Elixir Module should place in Foo in foo.ex, FooBar in foo_bar.ex, Foo.Bar in foo/bar.ex. From other forum posts, I think I should be using LightCodeInsightFixtureTestCase or CodeInsightFixtureTestCase, but I don't have any fixture to start out with since I'm testing file creation. https://devnet.jetbrains.com/message/5536514#5536514 is close, but I don't have a starting file from which the new file is created. I'm sure something used so much as testing New File is somewhere in the forums or documented since every custom language plugin would need it, but I'm not having any luck with the search.
Please sign in to leave a comment.
Hi Luke,
In IntelliJ IDEA, we usually do not test the user interface code. Because of that, you won't find any test framework support for testing the dialog itself. However, you certainly can, and should, test the actual file creation logic that happens in your action. You can use LightPlatformCodeInsightFixtureTestCase as the base class for your test: the fixture will set up a project and module for you, and you can create the file through your own code and verify the result through CodeInsightTextFixture.checkResultByFile().
How do I get an PSIDirectory from the myFixture or myModule so I can call createFile(String name, String templateName, PsiDirectory dir) In the actual IDE, using the debugger, I can see that CreateFormTemplateAction.actionPerformed(AnActionEvent) uses AnActionEvent.getDataContext -> LangDataKeys.IDE_VIEW.getData(dataContext) -> IdeView.getOrChooseDirectory() -> PsiDirectory, so can I get the IdeView in the test or some other method of getting a PsiDirectory? None of the methods that `.` shows me on myFixture or myModule in IntelliJ return a PsiDirectory.
CodeInsightTestFixture.getTempDirFixture().findOrCreateDir("") returns the root directory of the temporary in-memory project used by the tests. If you need a directory other than the root, you can pass a non-empty path to that method. Once you have a VirtualFile for the directory, you can use CodeInsightTestFixture.getPsiManager().findDirectory() to obtain the corresponding PsiDirectory.
Thanks you for that last post. I have the my action CreateElixiroduleAction.createFile returning an ElixirFile as expected, but checkResultsByFile is raising an exception.
Looking at the code, the myEditor is only needed when `ignoreTrailingWhitespaces` is `false`, so I
changed it to `true` and made sure to override `getTestDataPath()` and the test is working now: