Custom User Type with IUserType

36 views
Skip to first unread message

TheNephalim

unread,
Apr 5, 2018, 6:51:37 PM4/5/18
to nhusers
I was writing some unit tests for a custom type and everything was fine until I got to NullSafeSet:

        public void NullSafeSet(DbCommand cmd, object value, int index, ISessionImplementor session) {
            if (value == null) {
                ((IDataParameter)cmd.Parameters[index]).Value = DBNull.Value;
            } else {
                var yes = (bool)value;
                ((IDataParameter)cmd.Parameters[index]).Value = yes ? "Y" : "N";
            }
        }

It appears that on change NH-3431 there was a mass replace of the usage of System.Data classes to System.Data.Common classes.  The code moved from interfaces in the function signatures to concrete classes.  This makes it not possible to test this method, as far as I can tell, without actually trying to instantiate an instance of a command to pass into the method.

Does anyone have an idea of why this choice was made?  It is what it is at this point.  I'm just curious as to the reasoning.

- Robert

Alexander Zaytsev

unread,
Apr 5, 2018, 8:39:14 PM4/5/18
to nhu...@googlegroups.com
DbCommand is an abstract class. It should be testable the same way as the IDbCommand interface. 

The reason to change is that interfaces are immutable (at least in Microsoft's point of view on BCL) - once set it cannot have methods added or removed. The abstract classes are more fluid and can have additional methods (if the method itself is not abstract). The particular functions we were interested from abstract classes are the *Async methods which were added to the class hierarchy, but not to the interfaces.  

Best Regards,
Alexander

--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nhusers+unsubscribe@googlegroups.com.
To post to this group, send email to nhu...@googlegroups.com.
Visit this group at https://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.

Robert Eberhart

unread,
Apr 6, 2018, 9:50:16 AM4/6/18
to nhu...@googlegroups.com
Thank you for your response.

You are correct.

Upon reviewing your answer, the issue I'm having is less with DbCommand than with the DbParameterCollection.  I'm using Moq and when I try to mock/fake the DbCommand and set up the DbParameterCollection using a variety of standard techniques, Moq pitches a fit.

However, your answer combined with a review of the DbParameterCollection class gave me an idea.  I'm going to give it a shot and see if it works.

Sincerely,
Robert

--
You received this message because you are subscribed to a topic in the Google Groups "nhusers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/nhusers/39RnNkXgz78/unsubscribe.
To unsubscribe from this group and all its topics, send an email to nhusers+unsubscribe@googlegroups.com.

Robert Eberhart

unread,
Apr 6, 2018, 1:50:38 PM4/6/18
to nhu...@googlegroups.com
Hello,

This may not matter to other people, but I wanted to place this here just in case someone else has the same problem:

        [Fact]
        public void YesNoType_Returns_N_From_NullSafeSet() {
            var sessionImplementor = new Mock<ISessionImplementor>();
            var dbCommand = new Mock<DbCommand>();
            var dbParameterCollection = new Mock<DbParameterCollection>();

            var dbParameter = Mock.Of<DbParameter>();
            dbParameter.Value = string.Empty;

            dbParameterCollection.Protected()
                .Setup<DbParameter>("GetParameter", new object[] { It.IsAny<int>() }).Returns(dbParameter);

            dbCommand.Protected()
                .Setup<DbParameterCollection>("DbParameterCollection")
                .Returns(dbParameterCollection.Object);

            var type = new YesNoType();

            type.NullSafeSet(dbCommand.Object, false, 0, sessionImplementor.Object);
            dbCommand.Object.Parameters[0].Value.ShouldBe("N");
        }


I took a look at the DbParameterCollection and it has an abstract, protected GetParameter method which I was able to setup.

Thanks again for your help on this issue.

Roger Kratz

unread,
Apr 9, 2018, 7:30:28 AM4/9/18
to nhu...@googlegroups.com

IMHO, do not unit test/mock nhib types. Write integration tests for this instead.

--

To unsubscribe from this group and stop receiving emails from it, send an email to nhusers+u...@googlegroups.com.


To post to this group, send email to nhu...@googlegroups.com.
Visit this group at https://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "nhusers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/nhusers/39RnNkXgz78/unsubscribe.

To unsubscribe from this group and all its topics, send an email to nhusers+u...@googlegroups.com.


To post to this group, send email to nhu...@googlegroups.com.
Visit this group at https://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.

 

 

--

You received this message because you are subscribed to the Google Groups "nhusers" group.

To unsubscribe from this group and stop receiving emails from it, send an email to nhusers+u...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages