Writing tests for a context menu

The time has come for me to write some more tests to validate the menu items presented by the jet menu (not highlightings) - these items are project, file and line specific.

I need to have a test

  • load multiple project solution (already do this elsewhere)
  • Open a file
  • Move the caret to a specific area (e.g. a method declaration)
  • Invocate the menu
  • Assert the menu contents

My code is fighting me on this (it probably could do with a refactor) but is there any where in the Resharper codebase that tests context (jet) menus? I don't mean testing a MVC pattern....more an 'acceptance/integration' type test

Any tips ?

Comment actions Permalink

What do you mean by "jet menu"? Are you creating a custom context menu, or is it bulb items (quick-fix and context actions) that you want to test?

Comment actions Permalink

I need to improve my tests for an 'ActionHandler' that is context specific (the file, project and where in the file the caret is located). It then presents a menu to the user using the JetPopupMenus.

I have updated my highlighting test code to use DataContextOfTestTextControl to create a DataContext...I then call my Execute() method of the ActionHandler...what I'd like to do is capture the menu items of the JetPopupMenu and append them to the test output file to compare with the gold file. I think if I add my code into the existing action passed to ExecuteWithGold() then the gold file can contain highlightings and my custom action data...hope that makes sense

If I could force Shell.Instance.GetComponent<JetPopupMenus>().Create() to use a 'mock' then I might be able to confirm the outcome of the action. I suspect you're going to mention injecting the dependency...but before I do the refactoring for that is there another way...one that JetBrains have solved already...

I've the feeling this might be the first of many questions....apologies....and I look forward to your web talk next week...

Many thanks


Comment actions Permalink

Sorry for asking more questions, but is this an action handler that derives from ContextActionBase, or something custom? ContextActionBase is the recommended way of adding popup menus on specific parts of code, not tied to highlights - ReSharper will aggregate all instances of context actions for the current location, and display them in the same menu. It also has a base class for your tests to aid in testing availability and execution - if you create a new plugin + tests solution from the SDK, it creates an example context action, with example tests for you. Does this help?

Comment actions Permalink

I'd like to test two different things with gold files. Highlightings and separately the menu items presented by a keyboard shortcut which is not connected to the highlightings/actions.

I am currently looking at ShortcutsTest with DotPeek... it may be a way in for me. I see that it creates a ComponentContainer specific for the tests containing a test class.

Comment actions Permalink

OK. Highlights can be handled with the standard test base classes - you can add them from the SDK item templates.

And I gather then, that your popup menu is something similar to the Insert, Navigate To or Refactor This menus? Unfortunately, I can't find any examples of testing these popups. It might be best to test the items in your popup menu separately, and just have the main action display a menu that contains a group of actions and test it manually. It looks like the sub classes of GenerateTestBase do this - they're u. You could look at QuickFixContextActionHelper.ApplyCAQF, which appears to automat a JetPopupMenu, but I think this might be an integration test which actually shows the menu.

Also, with ShortcutsTest, it might be creating a container, but it doesn't have a solution or project files - if you want that, you'll have to use one of the other base classes, and that means not being able to inject anything into the container.

Comment actions Permalink

Thanks as always...the custom test container ended up being too many lines of code (with the need for mock classes too) so I have come up with a solution that I am not proud of :) but it works...I pass an action around for calling Show(). And it enables me to test the 'navigate to' menus with a gold file...

Normally in looks like this:
Action <JetPopupMenu, JetPopupMenu.ShowWhen> _menuDisplayer = (menu, showWhen) => menu.Show(showWhen);

but then in my tests I use...

(menu, when) =>



foreach (var itm in Enumerable.ToList(menu.ItemKeys).Cast<SimpleMenuItem>())


          textWriter.WriteLine("({0}) {1}", menu.Caption.Value, itm.Text );



Please sign in to leave a comment.