locked
Any tips on unit testing a C# VS2008 addin? RRS feed

  • Question

  • My addin mostly deals with instances of ProjectItem and TextDocument.  How would I go about mocking these objects in such a way that I can test my various manipulation of these objects?
    Saturday, February 27, 2010 6:44 AM

Answers

  • Hi mcaden,

    Thanks for your reply.

    ProjectItem, DTE.Window, TextDocument and EndPoint are all interfaces. To test this method, you can mock them by creating your own mock types and implementing those interfaces.

    For example, you can implement ProjectItem like following:

    public class MockProjectItem : ProjectItem
    {
        public string Name
        {
            get
            {
                return "Test.cs";
            }
        }
    
        ...
    }
    Hope it helps.

    Hongye Sun [MSFT]
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg @ microsoft.com

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    See what's happening in MSDN forum? Follow us at Twitter.
    • Marked as answer by McAden Wednesday, March 3, 2010 6:34 AM
    Wednesday, March 3, 2010 4:38 AM

All replies

  • Hi mcaden,

    Thanks for using MSDN forum. This is a great question.

    There is no good means for an add-in. For VSPackage, it provides facilitate to mock services.
    Please read the great article: http://dotneteers.net/blogs/divedeeper/archive/2008/02/07/LearnVSXNowPart11.aspx

    You'd better post the code about how did you get the ProjectItem and TextDocument. Thanks.

    Hongye Sun [MSFT]
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg @ microsoft.com

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    See what's happening in MSDN forum? Follow us at Twitter.
    Monday, March 1, 2010 9:52 AM
  • I haven't heard of VSPackage before.  I'll look into it.  Right now I'm just doing a Visual Studio 2008 Add-in.  


    Here's a function I'd like to have testing for.  The ProjectItem is retrieved either from the active window, or from iterating over the solution.
            protected void RemoveDuplicateBlankLines (ProjectItem projectItem, EnvDTE.Window ideWindow)
            {
                if (projectItem.Name.EndsWith(".cs") && !projectItem.Name.EndsWith("Designer.cs"))
                {
                    Debug.WriteLine("Removing Unnecessary Blank Lines: " + projectItem.Name);
                    try
                    {
                        TextDocument objTextDoc = (TextDocument)ideWindow.Document.Object("TextDocument");
                        EditPoint objEditPoint = objTextDoc.CreateEditPoint(objTextDoc.StartPoint);
                        while (!objEditPoint.AtEndOfDocument)
                        {
                            int secondFarthestLine = objEditPoint.Line + 2;
                            if (secondFarthestLine > objTextDoc.EndPoint.Line)
                            {
                                secondFarthestLine = objEditPoint.Line + 1;
                            }
    
                            if (objEditPoint.GetLines(objEditPoint.Line, secondFarthestLine).Trim() == string.Empty)
                            {
                                objEditPoint.DeleteWhitespace(vsWhitespaceOptions.vsWhitespaceOptionsVertical);
                                objEditPoint.Insert("\r\n");
                            }
    
                            objEditPoint.LineDown(1);
                        }
                    }
                    catch (Exception exc)
                    {
                        Debug.WriteLine(exc.ToString());
                        Debug.WriteLine("Removing Unnecessary Blank Lines failed, skipping");
                    }
                }
            }

    Tuesday, March 2, 2010 7:12 AM
  • Hi mcaden,

    Thanks for your reply.

    ProjectItem, DTE.Window, TextDocument and EndPoint are all interfaces. To test this method, you can mock them by creating your own mock types and implementing those interfaces.

    For example, you can implement ProjectItem like following:

    public class MockProjectItem : ProjectItem
    {
        public string Name
        {
            get
            {
                return "Test.cs";
            }
        }
    
        ...
    }
    Hope it helps.

    Hongye Sun [MSFT]
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg @ microsoft.com

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    See what's happening in MSDN forum? Follow us at Twitter.
    • Marked as answer by McAden Wednesday, March 3, 2010 6:34 AM
    Wednesday, March 3, 2010 4:38 AM