Wednesday, February 22, 2017

Fundamentals of Unit Testing: Unit Testing of IOC Code

Original: http://www.c-sharpcorner.com/UploadFile/dacca2/fundamentals-of-unit-testing-unit-testing-of-ioc-code/

This is the Fundamentals of Unit Testing article series. In this article we are discussing various important concepts of unit testing. You can read them here.
This article explains the advantages of IoC implementation in code. I hope we all have a basic concept of IoC and now we will learn how unit testing can be performed in IoC implemented code and then we will see to use a mock object in the same code.

Here is the code snippet that we will test. It's a small message sending operation and the implementation is like the following. 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Classes
{
    public interface ISendr
    {
        string send();
    }

    public class smsSender : ISendr
    {
        public string send()
        {
            return "SMS send";
        }
    }

    public class emailSender : ISendr
    {
        public String send()
        {
            return "Email send";
        }
    }

    public class voiceSend : ISendr
    {
        public string send()
        {
            return "VOICE Send";
        }
    }
    public class sender
    {
        public string send(ISendr obj)
        {
            return obj.send();
        }
    }
}

Ok, so now we will write the unit test for this unit of code. Have a look at the following example.

using System;
using Classes;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
namespace UNIT
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            Assert.AreEqual("SMS send"new sender().send(new smsSender()));
            Assert.AreEqual("Email send"new sender().send(new emailSender()));
            Assert.AreEqual("VOICE Send"new sender().send(new voiceSend()));
        }
    }
}

Since we have implemented all our classes like sendSMS, sendMAIL then we can send a concrete object of the implemented class as the argument of the send() function implemented in the IoC class. And if we run the test, we will see that all the tests passed, here is the output.



Now, let's assume that we did not implemente the ISender interface in any class. Then how will we create the mock object?

To create a mock object we will use the MOQ framework. So provide a reference of the MOQ framework in your project through the Nuget Package Manager.

Now, if we think that we did not implement the ISender interface in any class, then we can create one mock object of ISender and we can pass it as an argument of the send() function defined in the sender() method. Here is a sample implementation.

using System;
using Classes;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
namespace UNIT
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            var mock = new Mock<ISendr>();
            var objsender = new sender().send(mock.Object);
        }
    }
}

If we run the unit test application, we will see that the test passed.



Conclusion

In this article we saw how to implement unit testing of IoC implemented code. I hope it provides a basic understanding of unit testing when an application is implemented using IoC.

Monday, January 30, 2017

Deadlock view script


SELECT  L.request_session_id AS SPID, 
        DB_NAME(L.resource_database_id) AS DatabaseName,
        O.Name AS LockedObjectName, 
        P.object_id AS LockedObjectId, 
        L.resource_type AS LockedResource, 
        L.request_mode AS LockType,
        ST.text AS SqlStatementText,        
        ES.login_name AS LoginName,
        ES.host_name AS HostName,
        TST.is_user_transaction as IsUserTransaction,
        AT.name as TransactionName,
        CN.auth_scheme as AuthenticationMethod
FROM    sys.dm_tran_locks L
        JOIN sys.partitions P ON P.hobt_id = L.resource_associated_entity_id
        JOIN sys.objects O ON O.object_id = P.object_id
        JOIN sys.dm_exec_sessions ES ON ES.session_id = L.request_session_id
        JOIN sys.dm_tran_session_transactions TST ON ES.session_id = TST.session_id
        JOIN sys.dm_tran_active_transactions AT ON TST.transaction_id = AT.transaction_id
        JOIN sys.dm_exec_connections CN ON CN.session_id = ES.session_id
        CROSS APPLY sys.dm_exec_sql_text(CN.most_recent_sql_handle) AS ST
WHERE   resource_database_id = db_id()
ORDER BY L.request_session_id