dimanche 31 octobre 2021

Invoking a method with EF core dbContext to test with dummy data

I want to test a method (GetProjectInfo) from "AppManager" class using invoke,

AppManager.cs is as follows

public AppManager(Configuration configuration,
            AppClient client,
            Manager _manager,
            TicketCreator ticketCreator,
            EmailCatalog emailCatalogConfig,
            CommonConfiguration commonConfiguration,
            Entities entities,
            IBackgroundJobClient jobClient,
            FMSManager fmsManager)
        {
            _configuration = configuration;
            _manager = manager;
            _ticketCreator = ticketCreator;
            _commonConfiguration = commonConfiguration;
            _entities = entities;
            _jobClient = jobClient;
            _emailCatalogConfig = emailCatalogConfig;
            _client = client;
            _client.SetAuthenticationToken(GetAuthenticationToken());
            _fmsManager = fmsManager;
        }

In above class, Entities is nothing but a DBcontext EF core class,

 public partial class Entities : DbContext
    {

        public Entities()
        {
        }

        public Entities(DbContextOptions<Entities> options)
            : base(options)
        {
        }
      
.......All  DbSet properties
.......OnModelCreating(ModelBuilder modelBuilder)

So to test/invoke this method I have created a console app with below code

 static async Task Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            Assembly assembly = Assembly.LoadFile("dll path");
            Type type = assembly.GetType("AppManager");

            MethodInfo methodInfo = type.GetMethod("GetProjectInfo");

            ParameterInfo[] parameters = methodInfo.GetParameters();
            object[] parametersArray = new object[] { "939f378161c42d85658fb2d77f497e0f" };

            **var options = new DbContextOptionsBuilder<Entities>()
                           .UseInMemoryDatabase(databaseName: "db_name")
                           .Options;
            Entities _entities = new Entities(options);**

            object[] contructorArray = new object[] { null, null, null, null, null, null, _entities, null, null };

            object result = null;

            object classInstance = Activator.CreateInstance(type, contructorArray);

            result = methodInfo.Invoke(classInstance, parametersArray);

So to invoke the method from AppManager class I need to pass Entities to constructor param which I did as heighted bold above. But with this I am not able to connect to db.

Error I am getting here is,

System.MissingMethodException: 'Constructor on type 'AppManager' not found.'





Aucun commentaire:

Enregistrer un commentaire