How to check SqlParameter for equality

Using NUnit and NMock2, I could not compare what I thought was the same SqlParameters:

SqlParameter param1 = new SqlParameter("@Id", 1);
SqlParameter param2 = new SqlParameter("@Id", 1);
Assert.IsTrue(param1.Equals(param2));  // This failed

I came across this problem while trying to test method execution using NMock2

[Test]
    public void UpdateComments()
    {
        const int arbitraryId = 1;
        Comment comment = new Comment();

        SqlParameter idParam = new SqlParameter("@ChangeId", arbitraryId);

        Expect.Once.On(mockSqlDao).Method("ExecuteNonQuery")
              .With("usp_Update_Comment", idParam);

        changeDao.UpdateComment(arbitraryId, comment);

        mocks.VerifyAllExpectationsHaveBeenMet();
    }

I got this error:

NMock2.Internal.ExpectationException: unexpected call to sqlDao.ExecuteNonQuery ("usp_Update_Comment",) Expected: 1 time: sqlDao.ExecuteNonQuery (equal to "usp_Update_Comment", equal to <@ChangeId>) [called 0 times]

Questions:

  • How do you test with NMock2 when you expected parameter SqlParameter?
  • How do you compare the equality of two SqlParameters?
+3
source share
1 answer

.Equals() Equals, ( , SqlParameter "" SqlParameter, ), , .

Has.Property .With , , . :

Expect.Once.On(mockSqlDao).Method("ExecuteNonQuery")
          .With("usp_Update_Comment", Has.Property("ParameterName").EqualTo("@Id") & 
                                      Has.Property("Value").EqualTo(1));
+2

Source: https://habr.com/ru/post/1721707/


All Articles