About Delegate Invokation in Unit Testing in Visual Studio RRS feed

  • Question

  • User-1940827647 posted
     I have a class. In that class i have created a delegate, and invoked that in Test() method.
    Now, I am performing Unit Testing,
    When i invoke this Test() method, created delegate is becoming null.
    delegate void DelSendDataToBuffer(bool IsSend);
    DelSendDataToBuffer delSendDataToBuffer;
       UIBuffer objUIBuffer= new UIBuffer();
    how to write unit test method?  

    Thursday, October 20, 2016 7:08 AM

All replies

  • User-1838255255 posted

    Hi Santhosh ASP ocean,

    According to your description , I make a sample for Unit Test of delegate :

    1 . you need create a project name after UnitTestDemo ,you need modify class code like this  in program class :


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace UnitTestDemo
        delegate int NumberChanger(int one, int two);
        public class Program
            static void Main(string[] args)
            public static int Add(int pNum1, int pNum2)
                return pNum1 + pNum2;
            public static int All(int num1, int num2)
                NumberChanger nc = new NumberChanger(Add);
                return nc(10, 100);
    } } }

    2.you need create a UnitTest Project name after UnitTestDemoTest ,then add reference UnitTestDemo ,rename Program.cs to ProgramTest.cs , then modify code like this in ProgramTest.cs ,

    using System;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using UnitTestDemo;
    namespace UnitTestDemoTest
        public class ProgramTest
            public void AddTest()
                int a = 100;
                int b = 10;
                Assert.AreEqual(Program.All(a, b), 110);

    3. Now , nothing you can see ! Don't worry , build UnittestDemo project , you will see it :

    4.Right click Run selected Tests

    Best Regards,

    Eric Du

    Friday, October 21, 2016 7:20 AM
  • User303363814 posted

    how to write unit test method?
    In order to test code you will need to have some code.

    Where is the method that delSendDataToBuffer is being set to?

    Where is delSendDataToBuffer initialized (you have only shown the declaration)?

    Where is the code called that initializes delSendDataToBuffer?

    Sunday, October 23, 2016 11:57 PM