Test a SQL Insert .

1 Answer 35 Views
Non-public Mocking Static Mocking
shezad
Top achievements
Rank 1
shezad asked on 14 Jun 2021, 03:27 PM

Hi

I have a function:

    Public Function UpdatePasswordHistory(ByVal mods As DataSource, ByVal StaffCode As String, ByVal Password As String) As Integer


        Dim strSQl As New StringBuilder("Insert into [dbo].[PasswordHistory]([StaffCode],[Password],[DateSet])")
        strSQl.Append($"values('{StaffCode}','{EncryptPassword(UCase(Password))}','{System.DateTime.Today.ToString("yyyy-MM-dd")}')")


        Return mods.SqlDB.ExecuteNonQuery(strSQl.ToString())

    End Function

 

I want to create 2 tests.

The staffcode is a foreign key so must exists.

If no issues it should return 1. else 0.

My 2 tests always return 0.

I probably done this all wrong

   public void UpdatePasswordHistory_Staffcodeexists()
        {
            // Arrange
      var mockDS = Mock.Create<DataSource>();
            clsSecurity clsSecurity = Mock.Create<clsSecurity>();

            string Staffcode = "sahmed";
            string password = "test";
            Int32 rowsaffetced = 1;
            Int32 actualRowsaffected;

            Mock.Arrange(() => clsSecurity.UpdatePasswordHistory(mockDS, Staffcode, password)).Returns(rowsaffetced);

            actualRowsaffected = new clsSecurity().UpdatePasswordHistory(mockDS, Staffcode, password);

            Assert.AreEqual(rowsaffetced,actualRowsaffected);
        }

        [TestMethod]
        public void UpdatePasswordHistory_Staffcodedoesnotexist()
        {
            // Arrange
            var mockDS = Mock.Create<DataSource>();
            clsSecurity clsSecurity = Mock.Create<clsSecurity>();

            string Staffcode = "InvalidUser";
            string password = "test";
            Int32 rowsaffetced = 0;
            Int32 actualRowsaffected;

            Mock.Arrange(() => clsSecurity.UpdatePasswordHistory(mockDS, Staffcode, password)).Returns(rowsaffetced);

            actualRowsaffected = new clsSecurity().UpdatePasswordHistory(mockDS, Staffcode, password);

            Assert.AreEqual(rowsaffetced, actualRowsaffected);
        }

 

1 Answer, 1 is accepted

Sort by
0
Ivo
Telerik team
answered on 15 Jun 2021, 01:59 PM

Hello Shezad,

The tests are having just one small issue: the intended arrangement made on clsSecurity mock did not get called. The reason for that failure is not quite obvious, the arrangements are not related to the particular method but also to the mock object instance, so when you call a mocked method on a new object of the same type it will not get applied.

There are two options to handle this:

  • Add arrangement to handle newly created instances of a given type, in this case, it would be something like:
Mock.Arrange(() => new clsSecurity()).Returns(clsSecurity);
  • Use feature mocking, it means to add IgnoreInstance to the existing arrangements like this:
Mock.Arrange(() => clsSecurity.UpdatePasswordHistory(mockDS, Staffcode, password)).IgnoreInstance().Returns(rowsaffetced);

I hope the provided information helps. If you have any further comments and concerns do not hesitate to write us back.

Regards,
Ivo
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
Non-public Mocking Static Mocking
Asked by
shezad
Top achievements
Rank 1
Answers by
Ivo
Telerik team
Share this question
or