none
Unit test for Insert procedure RRS feed

  • Question

  • Insert function is already and it is a void method, I have to write unit test for it.
    Require get the data, which have just inserted, to compare with expected data.
    I can't get the data in unit test, I dont know this is possible or not. Please give me advise.
    Thursday, November 28, 2019 8:27 AM

All replies

  • Can you provide example from code? Reviewing it might help to determine how to make test for it or refactor code to better support unit testing.

    Thursday, November 28, 2019 9:54 AM
  • Insert function is already and it is a void method, I have to write unit test for it.
    Require get the data, which have just inserted, to compare with expected data.
    I can't get the data in unit test, I dont know this is possible or not. Please give me advise.

    The expected result on an Assert would mean that the void method would have to return data. The only unit test that you can do is verify that the method executed in a mock situation. You can look into using Moq which is a mocking framework. 

    http://www.blackwasp.co.uk/MoqTimes.aspx

    If you want to insert data and then verify that the data inserted correctly, then you have to do an integration test where you have a code that inserts the data into the database and code   that reads for the inserted data and does some sort of Assertion within a single integration test method that's using Nunit or some other unit testing framework. 

     



    • Edited by DA924x Thursday, November 28, 2019 10:03 AM
    Thursday, November 28, 2019 10:00 AM
  • Hi Hien_Hoang,

    Welcome to MSDN forum.

    Could you please share the type of database? If it is the data is from Access, you could refer this sample:

    using Microsoft.VisualStudio.TestTools.UnitTesting;
    
    namespace Datadriven
    {
        [TestClass]
        public class UnitTest1
        {
            [DataSource(@"System.Data.OleDb", "Provider=Microsoft.Ace.OLEDB.12.0; Data Source=D:\\Database1.accdb;", "Music", DataAccessMethod.Sequential)]
            [TestMethod()]
            public void AddIntegers_FromDataSourceTest()
            {
                string a = TestContext.DataRow["Name"].ToString();
                Assert.AreEqual("expected", a);
            }
    
            private TestContext testContextInstance;
            public TestContext TestContext
            {
                get { return testContextInstance; }
                set { testContextInstance = value; }
            }
        }
    }

    If datasource is Excel, you could use this connection string:

    [DataSource("System.Data.Odbc", "Dsn=Excel Files;Driver={Microsoft Excel Driver (*.xlsx)};dbq=D:\\Data.xlsx;defaultdir=.;driverid=790;maxbuffersize=2048;pagetimeout=5;readonly=true", "Sheet1$", DataAccessMethod.Sequential)]

    Look forward to your reply.

    Best Regards,

    Dylan


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    Friday, November 29, 2019 10:08 AM