Mock EnvDTE Solution always null

3 posts, 0 answers
  1. Jaime Weise
    Jaime Weise avatar
    25 posts
    Member since:
    Nov 2008

    Posted 31 Dec 2011 Link to this post

    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. Jaime Weise
    Jaime Weise avatar
    25 posts
    Member since:
    Nov 2008

    Posted 31 Dec 2011 Link to this post

    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. 

  3. DevCraft R3 2016 release webinar banner
  4. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 04 Jan 2012 Link to this post

    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 >>

Back to Top