Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7748881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:53:56+00:00 2026-06-01T10:53:56+00:00

I have a Visual Studio 2008 C# .NET 3.5 project that I am implementing

  • 0

I have a Visual Studio 2008 C# .NET 3.5 project that I am implementing unit tests for, but I’ve run in to a problem. My code references a 3rd party assembly that implements objects with internal constructors.

For example:

// in 3rd party assembly
public class Bar
{
    // internal constructor
    internal Bar();
    public int Id { get; }
    public string Name { get; }
    public Foo Foo { get; }
}

public class Foo
{
    // internal constructor
    internal Foo();
    public Collection<Bar> GetBars();
}

One method of mine that I would like to unit test is this:

// in my assembly
public static Bar FindByName(this Collection<Foo> c, string name)
{
    // search through the Foos to find the first bar with the matching name
}

And test it like this:

void TestMethod()
{
    Collection<Foo> foo_pool = new Collection<Foo>()
    {
        new Foo() { /*..*/ } // Error! ctor is inaccessible
    };

    Bar b = foo_pool.FindByName("some_name");
    assert_equal (b.Name, "some_name");
}

But, I can’t create objects of type Foo or type Bar. So, how can I unit test my method?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T10:53:58+00:00Added an answer on June 1, 2026 at 10:53 am

    For unit tests, you can use the PrivateObject class (namespace Microsoft.VisualStudio.TestTools.UnitTesting) to create objects with private constructors and even test private methods.

    http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.privateobject(v=vs.90).aspx

    http://www.gangleri.net/2007/11/15/PrivateObjects.aspx

    Here’s an example:

    [TestMethod]
    public void TestMethod2()
    {
        // Arrange
        var po = new PrivateObject(typeof(MyObject));
        var obj = (MyObject)po.Target;
        // Act
        var result = obj.Calculate(2);
        // Assert
        Assert.AreEqual(3, resul);
    }
    
    public class MyObject
    {
        internal MyObject()
        {
    
        }
    
        public int Calculate(int a)
        {
            return 1 + a;
        }
    }
    

    It uses reflection in the same way as Jim’s suggestion, but PrivateObject class encapsulates all the work to create the instance with private constructors.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with a C# ASP .NET project in Visual Studio 2008
I have in Visual Studio 2008 a .NET 3.5 C# project that uses a
I have a visual studio 2008 solution that includes an asp.net-hosted remoting project in
I have a Visual Studio 2008 .NET Framework 2.0 project that I'm building. It
I have a Visual Studio 2008 .NET 3.5 Web forms project that constantly shows
I have a VB.NET project in Visual Studio 2008 that I created a specialized
I have a WPF VB.NET project in Visual Studio 2008. For some reason, Visual
I have a C# .NET Compact Framework 3.5 mobile project in Visual Studio 2008.
I have a program written in VB.Net (Visual Studio 2008) that uses a DLL
I have Visual Studio 2008 and ASP.NET MVC2 project. To App_data folder I added

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.