This is a migrated thread and some comments may be shown as answers.

Mock EnvDTE Solution always null

2 Answers 68 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Jaime Weise
Top achievements
Rank 1
Jaime Weise asked on 31 Dec 2011, 06:59 AM
Mocking the DTE and child properties
Below I have created mocks for my dte objects so that I can test some extension methods:

var Dte = Mock.Create<DTE>();
var activeProject = Mock.Create<Project>();
var solution = Mock.Create<Solution>();
Mock.SetupStatic(typeof(RazorTemplateExtensions));

Mock.Arrange(() => Dte.Solution).Returns(solution);

Mock.Arrange(() => RazorTemplateExtensions.Dte).Returns(Dte);
Mock.Arrange(() => Solution.FileName).Returns("SolutionNameTest");
Mock.Arrange(() => ActiveProject.RootNamespace()).Returns("DefaultNamespace");
Mock.Arrange(() => Solution.ActiveProject()).Returns(ActiveProject);

The italicized arranges above work as I expect them to but when it comes time for testing the Solution object in the extensions class the solution instance is always assigned to null. If I stop in the immediate window and type Dte.Solution, I can see the proxy object as expected. But, when the solution variable is set in the below code the it is always set to null.
 

public static String DirectoryPath(this RazorTemplate razorTemplate)
{
    var solution = Dte.Solution;
     // while breaking here this solution is null
     // In the immediate window the Dte.Solution returns proxy object.
     ...
}

The only other clues that I have found are that I get this error when I try to access the ActiveProject() from the proxy object:

'EnvDTE.Solution' does not contain a definition for 'ActiveProject' and the best extension method overload 'Ellevate.T4Razor.Model.VisualStudioExtensions.SolutionExtensions.ActiveProject(EnvDTE.Solution)' has some invalid arguments

2 Answers, 1 is accepted

Sort by
0
Jaime Weise
Top achievements
Rank 1
answered on 31 Dec 2011, 01:50 PM
A couple of notes
  • It seems that when stepping through the code you don't see the values that will be used from the mock objects when you are in the real object functions. 
  • Also I noticed that the reason my results were not as expected is that later on in the extension method there was another extension method call where there was an "unmocked type that was being called". 
To make a long story short, everything worked as expected once I had taken care of all the loose ends. 

0
Ricky
Telerik team
answered on 04 Jan 2012, 02:32 PM
Hi jaime,

Thanks for reporting the issues.

On your first issue, yes stepping through the code you won’t see the real code unless you put a break-point in the target method itself (once the break-point is set, just hit F5 to skip). This happens if you do Mock.Create on a type or do Mock.Arrange on the method as JM is wrapping interceptor over the original code block.

Secondly, here I can understand that your extension method has a nested extension method that is failing the mocked one. In that regard, can you kindly send me an example of it so that I can investigate further?

Finally, it’s great to hear that you have made things work for you. But keep us posted on any further issues and we would love to help.


Kind Regards,
Mehfuz
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
General Discussions
Asked by
Jaime Weise
Top achievements
Rank 1
Answers by
Jaime Weise
Top achievements
Rank 1
Ricky
Telerik team
Share this question
or