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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:21:14+00:00 2026-06-13T08:21:14+00:00

The Microsoft Unit Test Wizard creates Accessor objects if you need to test a

  • 0

The Microsoft Unit Test Wizard creates Accessor objects if you need to test a non-public property from 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 public object, and the other takes the Accessor version. Since the Accessor is based on the public object I should be able to have one function. I had assumed that I could use Generics to accomplish with a simple cast. But after posting the question I discovered it was a lot more work, including having to update the underlying objects. My question is another approach to reduce these redundant methods to only having one function using casting (or another) approach?

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 have two dozen of these Unit Tests and the real objects have an average of 10 properties, I’ve simplified the examples here.

Here is the Accessor code that the Unit Test API creates (again, I have reduced it down to simplify the example):

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.ObjectModel;
using System.Data;

namespace NameHere.Bll
{
    [Shadowing("NameHere.Bll.Account")]
    public class Account_Accessor : ProjectBase_Accessor<Account>
    {
        protected static PrivateType m_privateType;

        public Account_Accessor(PrivateObject value);
        [Shadowing(".ctor@2")]
        public Account_Accessor(DateTime created, string createdBy);

        [Shadowing("_notes")]
        public string _notes { get; set; }

        public static Account_Accessor AttachShadow(object value);

        [Shadowing("Create@0")]
        public override void Create();
    }
}

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.ComponentModel;
using System.Linq.Expressions;

namespace NameHere.Bll
{
    [Shadowing("NameHere.Bll.ProjectBase`1")]
    public class ProjectBase_Accessor<T> : BaseShadow, INotifyPropertyChanged
    {
        protected static PrivateType m_privateType;

        public ProjectBase_Accessor(PrivateObject value);

        [Shadowing("Created")]
        public DateTime Created { get; set; }
        public static PrivateType ShadowedType { get; }

        [Shadowing("add_PropertyChanged@1")]
        public void add_PropertyChanged(PropertyChangedEventHandler value);
        public static ProjectBase_Accessor<T> AttachShadow(object value);

        [Shadowing("Create@0")]
        public virtual void Create();
    }
}
  • 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-13T08:21:15+00:00Added an answer on June 13, 2026 at 8:21 am

    The problem is that even though the accessor class exposes the same methods and properties as the class it shadows, there is no common interface between the accessor and the original class. Account_Accessor inherits from BaseShadow, Account inherits from something else. They are completely unrelated types as far as the compiler is concerned, not assignment compatible, so it will be difficult to pass instances of each into a common routine.

    If you could force the Account_Accessor class to implement an interface type that is also implemented by the Account, then you can pass instances of each to functions that take the interface type as the parameter. Like this:

    internal static IAccount SetupAccount(IAccount account, bool saveToDatabase)
    {
    // do account setup here - not construction
    }
    
    // to call: construct instance, then pass to common function
    var account = new Account(a, b);
    SetupAccount(account, true);
    

    If construction of the Account instance is complex enough that you want to have a common routine for that too, put type-specific wrappers in front of the common function:

    internal static IAccount CreateAccount(bool saveToDatabase)
    {
        var account = new Account(a,b);
        return SetupAccount(account, saveToDatabase);
    }
    
    internal static IAccount CreateAccountAccessor(bool saveToDatabase)
    {
        var account = new Account_Accessor(a,b);
        return SetupAccount(account, saveToDatabase);
    }
    

    One point you can’t escape is this: somebody somewhere has to commit to which instance to construct. Even if you boil it down to passing types around and using Activator.CreateInstance(), somebody has to commit to selecting which type to use.

    Once the instance is constructed, and both types implement a common interface, then all the common functions need to care about is the common interface.

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

Sidebar

Related Questions

Using Microsoft Unit Test Wizard, it creates Accessor objects if you need to test
I use Microsoft Visual Studio test Tools assembly for unit testing. And from one
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
Trying to unit test some simple code for a class project, however every time
I'm using VSTS 2K8 and I've set up a Unit Test Project. In it,
I'm learning VS Unit test and tried this: [TestMethod()] public void calcTest() { double
I'm using Visual Studio 2008 with Microsoft test tools. I need to access a
Is it possible to pass parameters to a Microsoft unit test DLL via the
I'm using Microsoft's Visual Studio Test Tools and Moq for unit testing. I have

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.