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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:56:06+00:00 2026-05-25T02:56:06+00:00

I am trying to create a unit test to verify that my method to

  • 0

I am trying to create a unit test to verify that my method to load AutoMapper configurations works correctly. My idea to load AutoMapper configurations is to put the configuration calls in classes with the following interface:

public interface IEntityMapConfiguration
{
    void ConfigureMapping();
}

To load these dynamically, I have the following loader class

public class EntityMapLoader
{
    public static void LoadEntityMappings()
    {
        // Load all IEntityMapConfiguration classes via reflection
        var types = AppDomain.CurrentDomain
                             .GetAssemblies()
                             .SelectMany(x => x.GetTypes())
                             .Where(x => x.IsSubclassOf(typeof(IEntityMapConfiguration)))
                             .Select(x => x as IEntityMapConfiguration)
                             .ToList();

        foreach (var config in types)
            config.ConfigureMapping();
    }
}

To verify this I have created the following unit test

public class UnitTestEntityViewModel
{
    public int Id { get; set; }
    public int Text2 { get; set; }
}

public class TestProjectionMapping : IEntityMapConfiguration
{
    public void ConfigureMapping()
    {
        Mapper.CreateMap<UnitTestEntity, UnitTestEntityViewModel>()
            .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id))
            .ForMember(dest => dest.Text2, opt => opt.MapFrom(src => src.Text));
    }
}

[TestClass]
public class AutomapperConfigurationTests
{
    [TestMethod]
    public void Loader_Loads_All_Configurations_In_Assembly()
    {
        // Setup
        UnitTestEntity entity = new UnitTestEntity { Id = 2, Text = "Test 1234" };

        // Act
        EntityMapLoader.LoadEntityMappings();

        // Verify
        UnitTestEntityViewModel result = Mapper.Map<UnitTestEntity, UnitTestEntityViewModel>(entity);
        Assert.AreEqual(entity.Id, result.Id, "View model's ID value did not match");
        Assert.AreEqual(entity.Text, result.Text2, "View model's text value did not match");
    }
}

The problem is that this unit test fails because it’s not picking up my TestProjectionMapping class, and I can’t figure out why. In my LoadEntityMappings() the types list has a count of zero. I have verified that my test assembly is returned from AppDomain.CurrentDomain.GetAssemblies().

I’m sure I am doing something obviously wrong, but I can’t find it. Any ideas?


Edit: I updated my LoadentityMappings() method to be:

    public static void LoadEntityMappings()
    {
        // Load all IEntityMapConfiguration classes via reflection
        var types = AppDomain.CurrentDomain
                             .GetAssemblies()
                             .SelectMany(x => x.GetTypes())
                             .Where(x => x.GetInterfaces().Contains(typeof(IEntityMapConfiguration)))
                             .Select(x => x as IEntityMapConfiguration)
                             .ToList();

        foreach (var config in types)
            config.ConfigureMapping();
    }

This is still failing. The statement (without the final select) is returning my TestProjectionMapping class, but the x as IEntityMapconfiguration is failing. In the debugger when I type (IEntityMapConfiguration)types[0] I get the following error:

Cannot cast 'types[0]' (which has an actual type of 'System.RuntimeType') to 'MyJobLeads.DomainModel.EntityMapping.IEntityMapConfiguration'

For reference in the watch list typing types[0] shows:

types[0] {Name = "TestProjectionMapping" FullName = "MyJobLeads.Tests.TestProjectionMapping"} System.Type {System.RuntimeType}

Also, types[0] is IEntityMapConfiguration is false

I’m not sure why I can’t do this cast, any ideas?

  • 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-05-25T02:56:07+00:00Added an answer on May 25, 2026 at 2:56 am

    You can’t use Type.IsSubclassOf to see if a type implements an interface. You have to use Type.IsAssignableFrom (or you can use is IEntityMapConfiguration or Type.GetInterfaces and then see if the resulting sequence contains typeof(IEntityMapConfiguration).

    Now think about why you can’t use Type.IsSubclassOf to see if a Type implements an interface. Because we don’t say that a class that implements an interface is a subclass of that interface; instead we say that it implements the interface, and we reserve is subclass of for its base class (object or ValueType unless one is specified).

    By the way, this is clearly spelled out in the documentation for Type.IsSubclassOf:

    The IsSubclassOf method cannot be used to determine whether an interface derives from another interface, or whether a class implements an interface.

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

Sidebar

Related Questions

I am trying to create a unit test for a method that takes a
I'm trying to write a unit-test to verify that it's impossible to create an
Specifically, I'm trying to create a unit test for a method which requires uses
Working with Test::Unit and Shoulda. Trying to test Users.create . My understanding is that
I am trying to create a unit test that makes sure all of my
I am trying to create a unit test that generates an empty test document
I'm trying to create a unit test to test the case for when the
I am trying to create a unit test similar to how I would have
I'm trying to create an in-process unit test for my service to client interactions
I'm trying to create a mock HttpContextBase for unit test. var fakePrinciple = new

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.