4

Using NUnit and NMock2 I was not able to compare what I thought were the same SqlParameters:

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

I stumbled across this problem, when trying to test an execution of a method 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 received this error:

NMock2.Internal.ExpectationException: unexpected invocation of 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 is SqlParameter?
  • How do you compare equality of two SqlParameters?
sgmeyer
  • 645
  • 5
  • 21
  • 1
    I would guess that SqlParameter.Equals is an object equality test, so as not to define for you what it would mean for two parameters to be 'equal' in some other sense. There are lots of fields inside the SqlParameter; if it is sufficient that the column name and the value match, you could test those directly. Can't comment on NMock though. – Mikeb Nov 02 '09 at 16:05
  • I suspect the NMock2 is failing because it see the calling parameter as different from the expected parameter. I would have expected the other SqlParameter properties would be initialized with the same default. This may or may not be true. Also a side note here more information about the method call; changeDao.Updatecomment(string proc, params SqlParameter[] parameters)... – sgmeyer Nov 02 '09 at 16:13

1 Answers1

3

Because .Equals() is using the default implementation of Equals as far as I know (which means that a SqlParameter will only "equal" another SqlParameter if they are the same object), you will need to directly interrogate the properties of the parameter to ensure the correct data is being passed.

The Has.Property call within .With allows you to check the properties of a parameter without requiring that a parameter equals some other value. Try the following:

Expect.Once.On(mockSqlDao).Method("ExecuteNonQuery")
          .With("usp_Update_Comment", Has.Property("ParameterName").EqualTo("@Id") & 
                                      Has.Property("Value").EqualTo(1));
Ryan Brunner
  • 14,723
  • 1
  • 36
  • 52