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 8713641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:23:33+00:00 2026-06-13T05:23:33+00:00

Using Microsoft Unit Test Wizard, it creates Accessor objects if you need to test

  • 0

Using Microsoft Unit Test Wizard, it creates Accessor objects if you need to test a non-public property in another project. Inside my Unit Tests I create helper functions so that I don’t repeat the same code just in every Unit Test method. Currently I have two tests that are almost identical except one takes a standard object, and the other takes the Accessor version. Since the Accessor is based on the standard version I should be able to have one function and I assume I should be able to use Generics to accomplish. The issue is trying to retype and compile failures.

Here are the existing two functions:

// Common function to create a new test record with standard Account object
internal static void CreateAccount(out Account account, bool saveToDatabase)
{
    DateTime created = DateTime.Now;
    string createdBy = _testUserName;

    account = new Account(created, createdBy);

    account.Notes = Utilities.RandomString(1000);

    if (saveToDatabase)
        account.Create();
}

// Common function to create a new test record with Account_Accessor
internal static void CreateAccount(out Account_Accessor account, bool saveToDatabase)
{
    DateTime created = DateTime.Now;
    string createdBy = _testUserName;

    account = new Account_Accessor(created, createdBy);

    account.Notes = Utilities.RandomString(1000);

    if (saveToDatabase)
        account.Create();
}

I tried changing the signature to of a combined function to:

internal static void CreateAccount<T>(out T account, bool saveToDatabase) {...}

but couldn’t get recast T properly to Account or Account_Accessor. Any suggestions?

  • 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-13T05:23:34+00:00Added an answer on June 13, 2026 at 5:23 am

    You should add constraint to the generic function, because of this two methods:

    account.Notes = Utilities.RandomString(1000);
    account.Create();
    

    I suggest you to add some interface with this two methods and add inheritance from it to your two classes.
    Constraint should be as follows:

    where T : YourNewInterface
    

    About constraints you can read at http://msdn.microsoft.com/en-us/library/bb384067.aspx

    UPDATE

    public interface IAccount
        {
            string Notes { get; set; }
            void Create();
            void Init(DateTime created, string createdBy);
        }
    
    public class Account : IAccount
    {
        public string Notes
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
    
        public void IAccount.Create()
        {
            throw new NotImplementedException();
        }
    
        void IAccount.Init(DateTime created, string createdBy)
        {
            throw new NotImplementedException();
        }
    }
    
    public class Account_Accessor : IAccount
    {
    
        string IAccount.Notes
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
    
        public void IAccount.Create()
        {
            throw new NotImplementedException();
        }
    
        void IAccount.Init(DateTime created, string createdBy)
        {
            throw new NotImplementedException();
        }
    }
    
    
    class Program
    {
        internal static void CreateAccount<T>(out T account, bool saveToDatabase) where T : IAccount,new()
        {
            DateTime created = DateTime.Now;
            string createdBy = _testUserName;
    
            account = new T();
            account.Init(created, createdBy);
    
            account = (T)Activator.CreateInstance(typeof(T), new object[] { created, createdBy });
    
            account.Notes = Utilities.RandomString(1000);
    
            if (saveToDatabase)
                account.Create();
        }
        static void Main(string[] args)
        {
            Account acc;
            Account_Accessor acc2;
            CreateAccount(out acc, false);
            CreateAccount(out acc2, false);
        }
    }
    

    Here is some comments about my example:
    1. I’ve replaced CreateInstance by adding new() constraint.
    2. Because new() constraint can’t have parameters because of .NET generic limitations, I’ve added Init() method to the IAccount interface.
    3. Init method should not be called by client code of the Account class, that’s why we define the method as private and explicitly for IAccount.
    4. Because of new() constraint you should provide parameterless constructor for Account. If you do this, your client code should not call this parameterless ctor.

    As for me I’d leave Activator.CreateInstance as is. It is good workaround for the limitations of generic new() constraint

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

Sidebar

Related Questions

The Microsoft Unit Test Wizard creates Accessor objects if you need to test a
We're using Microsoft's Unit Test program and we use the Unit Test Wizard to
In my project I write tests using Microsoft's unit testing framework. All of my
I'm using VSTS 2K8 and I've set up a Unit Test Project. In it,
I'm using Visual Studio 2008 with Microsoft test tools. I need to access a
The project I'm working on has a bunch of service-tier unit tests using Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute.
I'm using Microsoft's Visual Studio Test Tools and Moq for unit testing. I have
I need to unit test a method that is using the System.Net.WebClient in the
When I create a test using MS Visual Studio's builtin unit test wizard it
I'm using C# 4.0. I'm running a unit test using Microsoft.VisualStudio.TestTools.UnitTesting . My UnitTest

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.