2

In my package I am using (DTE) GetService(typeof (DTE)) to get information about the currently opened solution. Is there a way to simulate this for a test, particularly so that I can build using dte.Solution.SolutionBuild?

Code in main package class:

var solutionModel = new SolutionModel(((DTE) GetService(typeof (DTE))).Solution);

SolutionModel class (stripped back):

public class SolutionModel
{
    private readonly Solution _packageSolution;

    public SolutionModel(Solution solution)
    {
        _packageSolution = solution;
    }

    public SolutionModel() {} // This constructor is used for tests so _packageSolution will be null

    public bool Build()
    {
        if (_packageSolution != null)
        {
            var buildObject = _packageSolution.SolutionBuild;
            buildObject.Build(true);
            return buildObject.LastBuildInfo == 0;
        }

        return ManualCleanAndBuild(); // current messy alternative way of doing the build for tests
    }
}

So I want to be able to use the _packageSolution build rather than ManualCleanAndBuild() in my tests.

2 Answers2

2

Assuming that you are referring to integration tests (and not to unit tests) where you need to load your package in a real Visual Studio instance, it depends on the testing framework that you are using. If you are using MSTest with the VSIDE Host Adapter (the integration test project that the package wizard creates if you mark the checkbox in the last page of the wizard) there is a Utils.cs file that uses the static VsIdeTestHostContext class to get the DTE instance or services:

   public static class VsIdeTestHostContext
   {
      [CLSCompliant(false)]
      public static DTE Dte { get; }
      public static IServiceProvider ServiceProvider { get; set; }
   }

If you want to learn the inners of the VS IDE Host Adapter I think that the VS 2008 SDK was the last SDK that provided the source code and the documentation (http://msdn.microsoft.com/en-us/library/bb286982%28v=vs.90%29.aspx)

Carlos Quintero
  • 4,300
  • 1
  • 11
  • 18
  • Thanks for this answer, I'd completely not noticed that it had generated test projects for me! I'll have a go at this next week and check it all works for me before I mark this as the answer. Thanks :) – Laura Armitage Nov 03 '14 at 14:30
  • Unfortunately these integration test samples created by the wizard are gone in vs2015. Could anyone post these online? – David Feb 20 '16 at 12:49
0

The way I ended up 'solving' this was to mock EnvDTE.Solution instead (seems like it can only be done in the Package_IntegrationTests project which is created for you - you can't reference EnvDTE in any other project). I couldn't figure out how to use the methods in Utils.cs as suggested by Carlos below to open my existing solutions.