-3
public List<TempProject> GetActiveProjects()
{
    foreach (Project project in _applicationObject.DTE.Solution.Projects)
    {
        if (project.FullName.EndsWith(".csproj"))
            projects.Add(new TempProject(project));
    }
    return projects;
}
Mark Seemann
  • 225,310
  • 48
  • 427
  • 736

2 Answers2

1
  1. Open Visual Studio
  2. Create a new library project (let's assume C#)
  3. Add appropriate references to a unit testing framework (from the tags, it would seem that you'd want NUnit - the easiest way to get it is to pull it from NuGet).
  4. Add a class to hold the unit tests for the code in the OP.
  5. Adorn the test class with the [TestFixture] attribute.
  6. Add a new public method that returns void and takes no parameters. This will be your test method.
  7. Adorn the test method with the [Test] attribute.
  8. Write the unit test in the body of the test method.
  9. Repeat from 4. until you have enough unit tests.
Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
0
  1. Provide mocked instance of _applicationObject to class you are testing. This will allow you to setup different projects set (google for dependency inversion, mocking, Moq).
  2. Write test which verifies that empty list is returned when there is no projects in solution.
  3. Write test which verifies that empty list is returned if there is no C# projects in solution.
  4. Write test which verifies that all C# projects are added to result.

BTW consider to depend on solution object instead of application object, if you only need to get data from solution. That will allow you to easily mock projects set and avoid train wreck when getting projects:

foreach (Project project in _solution.Projects)
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459