Friday 18 October 2019

Using xunit and moq in .net core for Testing




Everyone wants to write bug free code which is good but the problem is there are few bugs which are revealed at the time of integration or in future after updating code that has dependencies.

Unit Tests help us prevent such situation right after you are done with your changes. UTCs cannot catch all the errors  but they minimize the bugs from production

You can test your code with some sample data right after you write it. I have worked on software which has more than 10000 UTCs and most part of the application can be tested using those UTCs only. These UTCs catch integration level error or check some corner scenarios.

UTCs are as good as their writer. It all depend on how many scenarios you can cover and quality of Test Data.
It’s time for example-

First add a xUnit Test project in your solution



This will create a UnitTest1.cs file in your project with Test1() method. In xUnit project Fact and Theory are used in place of TestMethod attribute
Now it’s time to include moq nugget



After importing it in your class you are all set for your first test method

using Xunit;
using CrossProjectDemo;
using Moq;

namespace Struts.Tests
{
    public class UnitTest1
    {
        [Fact]
        public void Test1()
        {
            var moc = new Mock<CustomLoops>();
            var car = new Car(moc.Object);
            car.Caller(null, 9);
        }
    }
}

Here you can see I am using Mock class to create a dummy object or class CustomLoops which is called mocking a class
Let’s see what this moc variable has



Now It’s time to run the Mock method as well

        [Fact]
        public void Test1()
        {
            var moc = new Mock<CustomLoops>();
            moc.Setup(mc => mc.mockingMethod()).Returns(200);
            var car = new Car(moc.Object);
            car.Caller(null, 9);

            Assert.Equal(200, car.mockingMethod());
        }
As you can see UTC ran successfully and Assert passed, Now see one thing
        public virtual int mockingMethod()
        {
            return 110;
        }

The mocking method is returning only 110. So how can the Assert pass.

In this example I have mocked the method mockingMethod of CustomLoops class and return 200 from it. To mock a method it must be virtual.
Now coming back to attributes. xUnit uses Fact and Theory as attribute to mark a method test


  •          Fact is used when you want to run a method only once
  •         Theory is used when you want to run a method multiple times
  •         You can use InlineData() attribute to pass various data to the test method

Now you know you can mock classes and methods in .net using moq and xUnit. These are very powerful library for TDD. With use of dependency injection testing can be done more effectively and it also reduces dependency among projects.

That’s it from this post
Thanks

Thursday 10 October 2019

Setting and getting private variable of a class without properties



Setting or getting private variable is quite a corner scenario while developing a new application and mainly developer will always find a way to access the private variable. But there is a direct way where we can access the private variable using reflection without much of hassle

What is the real-world scenario where I would need to access a private variable?
In real-world scenario I don’t you may ever need to access private variable in your own application. If you do then you’ll simply change it access specifier.

     UTC or TDD development you might find yourself in scenario where you need to access or change the value of private variable of a class. In UTCs we often do such things to by check some negative scenario or for some other reasons.

      Third party dll or library, we may need to access private variable of a third-party variable

These are the cases where I need to access private variable

How could we achieve this?
Quite simple, you just need to do some R&D with reflection classes and need to spend some time with Type class provided in .net framework.

Example-
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CrossProjectDemo
{
    public class Car
    {
        private string name = string.Empty;
        private string prvtVariable = "PrivateVariable";

        public string Name {
            get { return name; }
            set { name = value; }
        }

        public Car()
        {
        
        }
    }
}

As you can see there two private variables
1. name
2.
prvtVariable

While name is being accessed in a public Property Name, prvtVariable will be accessed directly

Let’s see how
First in your application import Reflection namespace
using System.Reflection;

Then follow the code-
Car c = new Car();
Type typ = typeof(Car);
FieldInfo type = typ.GetField("prvtVariable", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var value = type.GetValue(c);



In above image you can see that the type.IsPrivate is true which means variable is private



And we can access its value into the value variable.
That’s easy right.
Now, Let’s move towards how we can change its value. Lets’ make few changes to the existing code
Just add one-line right after Getvalue() method call

var value = type.GetValue(c);
type.SetValue(c, "Not so much");

and now you have changed the value of a private variable



Easy, isn’t it. So here we’re accessing private variables through reflection.
For more detail info like this visit this
Thanks

Monday 7 October 2019

How Generic Dictionary stores data (Custom Dictionary)


Dictionary is powerful data structure to maintain data in key value pairs. Due to this key value pair dictionary becomes quite useful in coding. Developer can fetch only the data which is corresponding to a specific key.
A dictionary can have duplicate record, but key must be unique.
How dictionary maintain data and how it performs operation?
A dictionary uses linked lists to maintain data. Consider you are using a generic dictionary<string, ‘AnyType’>. Dictionary sorts and search data based on the key, now will it sort data if key is string type, for obvious reasons sorting and searching on string will be quite slow as compared to int or other enum types.
So, how does dictionary perform these operations so quickly?
Well, quite simple. It uses hash code. Dictionary converts the key value into its corresponding hash code and store only that hash code. Now searching and sorting on this hash code becomes fast.
How does it do it, practically?
Just like linked list, let’s create a Node class. To understand following code better I would say please go and watch how linked list is implemented here
  1. namespace Generic    
  2. {  

  3. public class Node<T>  
  4.   
  5. {  
  6.   
  7. public T data;  
  8.   
  9. public Node(T data)  
  10.   
  11. {  
  12.   
  13. this.data = data;  
  14.   
  15. }  
  16.   
  17. }  
  18.   
  19. }  


Now to maintain key value pair and to link the objects we need to create another class
  1. namespace Generic  
  2.   
  3. {  
  4.   
  5. public class HashNodeMap<T>  
  6.   
  7. {  
  8.   
  9. public int key;  
  10.   
  11. public Node<T> data;  
  12.   
  13. public HashNodeMap<T> next;  
  14.   
  15. }  
  16.   
  17. }  
Above, key will be used to store hash code of each key, node will store the actual data and next will help us link the new item

  1. namespace Generic  
  2.   
  3. {  
  4.   
  5. public class GenericHashDictionary<Tkey,Tdata>  
  6.   
  7. {  
  8.   
  9. public HashNodeMap<Tdata> hashNode = null;  
  10.   
  11. public int Count;  
  12.   
  13. public object this[Tkey s]  
  14.   
  15. {  
  16.   
  17. get  
  18.   
  19. {  
  20.   
  21. return FindHashCode(s).data;  
  22.   
  23. }  
  24.   
  25. set  
  26.   
  27. {  
  28.   
  29. // You logic to update the item found  
  30.   
  31. // Ex- hashNode[“key”] = ‘AnyType’;  
  32.   
  33. }  
  34.   
  35. }  
  36.   
  37. // Generic method to create new node every time an item is added in custom dictionary  
  38.   
  39. private HashNodeMap<Tdata> CreateNode(Tkey key, Tdata data)  
  40.   
  41. {  
  42.   
  43. int hashCode = key.GetHashCode();  
  44.   
  45. HashNodeMap<Tdata> newNodeMap = new HashNodeMap<Tdata>();  
  46.   
  47. newNodeMap.data = new Node<Tdata>(data);  
  48.   
  49. newNodeMap.key = hashCode;  
  50.   
  51. return newNodeMap;  
  52.   
  53. }  
  54.   
  55. // Method to add new item in custom dictionary  
  56.   
  57. public void Add(Tkey key, Tdata data)  
  58.   
  59. {  
  60.   
  61. HashNodeMap<Tdata> current = null;  
  62.   
  63. if (hashNode == null)  
  64.   
  65. {  
  66.   
  67. hashNode = CreateNode(key, data);  
  68.   
  69. }  
  70.   
  71. else  
  72.   
  73. {  
  74.   
  75. current = hashNode;  
  76.   
  77. while (current.next != null)  
  78.   
  79. {  
  80.   
  81. current = current.next;  
  82.   
  83. }  
  84.   
  85. current.next = CreateNode(key, data);  
  86.   
  87. }  
  88.   
  89. Count++;  
  90.   
  91. }  
  92.   
  93.   
  94. private Node<Tdata> FindHashCode(Tkey key)  
  95.   
  96. {  
  97.   
  98. Node<Tdata> data = null;  
  99.   
  100. int hashCode = key.GetHashCode();  
  101.   
  102. HashNodeMap<Tdata> current = hashNode;  
  103.   
  104. while (current != null)  
  105.   
  106. {  
  107.   
  108. if (current.key == hashCode)  
  109.   
  110. {  
  111.   
  112. data = current.data;  
  113.   
  114. break;  
  115.   
  116. }  
  117.   
  118. if (current.next != null)  
  119.   
  120. {  
  121.   
  122. current = current.next;  
  123.   
  124. }  
  125.   
  126. }  
  127.   
  128. return data;  
  129.   
  130. }  
  131.   
  132. }  
  133.   
  134. }  
Above are some basic operation performed in a standard dictionary.

Now time to use this
  1. GenericHashDictionary<stringstring> hdt = new GenericHashDictionary<stringstring>();  
  2. hdt.Add("userName""ishoo");  
  3. hdt.Add("password""1234");  
  4. hdt.Add("email""ishooagarwal");  
 
As you can see the (-ve) numbers these are keys which I used. 
I am still developing this data structure and implementing various things in it. Currently I am implementing IEnumerable and IEnumerator so that foreach loop can be performed on this custom dictionary. 
In my next blog I will show you how to to sort remove and iterate through the custom dictionary.
Hope you enjoy it and find it helpfull
Thanks 

Send All Azure App Insights Or Logs Data To Tool Like Datadog

  Introduction Microsoft Azure provides a variety of insights and monitoring logs that you can monitor to check the state of a resource and ...