Use Dynamic objects to return different Types from a Method

2k Views Asked by At

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 standard version 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. This was confirmed when I asked if any other approach existed. It was suggested that Dynamic objects might work. I'm not familiar with Dynamic objects, could someone post an example that allow me to have one shared function and determine the object at run time?

Here are the existing two functions:

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

    Account 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 Account_Accessor CreateAccount(bool saveToDatabase)
{
    DateTime created = DateTime.Now;
    string createdBy = _testUserName;

    Account_Accessor 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

There are 1 best solutions below

0
On

Here my first attempt at a shared function using Dynamic objects. It returns either an Account or Account_Accessor depending on the boolean useAccessor param:

internal static dynamic CreateAccount(bool saveToDatabase, bool useAccessor)
{
    DateTime created = DateTime.Now;
    string createdBy = _testUserName;
    dynamic account;

    if (useAccessor)
         account = new Account_Accessor(created, createdBy);
    else
         account = new Account(created, createdBy);    

    account.Notes = Utilities.RandomString(1000);

    if (saveToDatabase)
        account.Create();
}

Any suggestions or comments on this approach?