Error while doing writing unit test case -Could not load file or assembly 'Oracle.DataAccess, Version=4.112.4.0

折月煮酒 提交于 2020-01-06 07:28:09

问题


We have a requirement to do unit testing using either MS Test or NUnit Framework for our C# code. There are many methods which includes DB call. When trying to do so, I am getting below error:

*{"Could not load file or assembly 'Oracle.DataAccess, Version=4.112.4.0, Culture=neutral, PublicKeyToken=*******' or one of its dependencies. An attempt was made to load a program with an incorrect format."}*

I checked the Oracle version to make sure there is no mismatch, but no success. Also read couple of articles that instead of making DB call one should be using mocking to bypass DB. But with this limitation the coverage is very minimal. Could anyone please guide to fix the above error. Adding the sample code which i tried just for POC

public class Class1
    {
        public IEnumerable<ModelClass> GetListOfExceptions()
        {
            OracleConnection conn = new OracleConnection(Constants.ConnectionString);
            try
            {
                conn.Open();
                OracleCommand cmd = new OracleCommand(Constants.<Oracle Query>, conn);
                cmd.CommandType = CommandType.Text;
                //cmd.CommandType = CommandType.StoredProcedure;
                OracleDataReader dr = cmd.ExecuteReader();
                ModelClass modelException = null;
                List<ModelClass> lst = new List<ModelClass>();
                while (dr.Read())
                {
                    modelException = new ModelClass();
                    modelException.POLICYNO = Convert.ToString(dr.GetValue(1));
                    modelException.PREMIUM = Convert.ToInt32(dr.GetValue(2));
                    modelException.AGE = Convert.ToInt32(dr.GetValue(3));
                    lst.Add(princeException);
                }
                return lst;

            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                conn.Close();
            }

        }


    }

    [TestFixture]
    public class Class1Tests
    {
        [Test]
        public void GetListOfExceptionsTest()
        {
            Class1 obj = new Class1();
            obj.GetListOfExceptions();

        }
     }

回答1:


Most likely the ODP.NET (i.e. Oracle.DataAccess) is not installed on your machine. Or maybe it is installed but you did not install any Oracle Client (e.g. Oracle Instant Client)

Go to this page Oracle Data Access Components - .NET Downloads and download and install the Oracle Data Provider for .NET. Ensure that you download the same version as your Oracle Client in case you installed already one. Ensure also that you have the same architecture, i.e. 32-bit vs. 64-bit as your application.




回答2:


Finally the issue is fixed after installation to Oracle Client 12.1 (32 bit version).



来源:https://stackoverflow.com/questions/49490287/error-while-doing-writing-unit-test-case-could-not-load-file-or-assembly-oracl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!