Wednesday, December 16, 2020

[Code Snippet] : Connect to Sql Server from ArcObject

Be The First To Comment

 using System;

using ESRI.ArcGIS.DataSourcesFile;

using ESRI.ArcGIS.esriSystem;

using ESRI.ArcGIS.Geodatabase;


namespace MyNamespace

{

  class MyProgram

  {

    private static LicenseInitializer _licenseInitializer = new LicenseInitializer();


    [STAThread()]

    static void Main(string[] args)

    {

      _licenseInitializer.InitializeApplication(new esriLicenseProductCode[] { esriLicenseProductCode.esriLicenseProductCodeEngineGeoDB },

                                                   new esriLicenseExtensionCode[] { });


      Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.SqlWorkspaceFactory");

      IPropertySet propertySet = new PropertySetClass();

      propertySet.SetProperty("SERVER", "servername");

      propertySet.SetProperty("INSTANCE", "sde:sqlserver:servername");

      propertySet.SetProperty("AUTHENTICATION_MODE", "DBMS");

    // propertySet.SetProperty("AUTHENTICATION_MODE", "OSA");

      propertySet.SetProperty("DATABASE", "test1");

      propertySet.SetProperty("USER", "username");

      propertySet.SetProperty("PASSWORD", "password");

      propertySet.SetProperty("VERSION", "sde.DEFAULT");


      IWorkspaceFactory workspaceFactory = new SDCWorkspaceFactory();

      IWorkspace workspace = workspaceFactory.Open(propertySet, 0);

    }

  }

}


Friday, December 11, 2020

[Code Snippet] Assert.Throws on Exception and Derived Type Nunit

Be The First To Comment

 Code snippet for asserting throw in ArgumentException and its derived types(such as ArgumentNullException) in Nunit - 

public void Test(){

Assert.Throws(Is.InstanceOf<ArgumentException>(), () =>{

    //SUT code

});

}

Monday, May 11, 2020

[Code snippet] How to set value of private variable in Unit Test Using Reflection ?

Be The First To Comment
A sample example to set private variable to true from unit test class in C#.Net

//TestService.cs
public class TestService
{
        private bool _isInitialized = false;
}



//TestServiceUnitTest.cs
using System.Reflection;

public class TestServiceUnitTest
{
          private TestService _testService;
       
           [TestInitalize]
          private void testInitalize()
         {
            _testService = new TestService();
         }
       
         [TestMethod]
         Private void SetInitializeToTrue()
         {
                         FieldInfo field = typeof(TestService).GetField("_isInitialized",  BindingFlags.NonPublic |       BindingFlags.Instance);

          field.SetValue(_testService , true);

         }
}

Wednesday, April 22, 2020

[Code Snippet] Assert Exception in private methods using PrivateObject

1 Comment
NON ASYNC Method

Method.cs

private MyMethod(object value1)
{
      if(value1 == null)
      {
           throw new ArgumentNullException(nameof(MyMethod));
      }
}

Test.Cs

[TestMethod]
public void MyMethod_Throws_Verify()
{
         PrivateObject po = new PrivateObject(new Method())

TargetInvocationException exception = Assert.ThrowsException<TargetInvocationException>(() =>
                    privateObject.Invoke("MyMethod", new object[] { null }));

            Assert.AreEqual(typeof(ArgumentNullException), exception.InnerException.GetType());
       
}


 

© 2011 GIS and Remote Sensing Tools, Tips and more .. ToS | Privacy Policy | Sitemap

About Me