Я создал тип пользователя для System.Drawing.Color, и вот как я тестировал его с помощью MSTest и Moq .
ColorUserType.cs:
public class ColorUserType : IUserType
{
public object Assemble( object cached, object owner )
{
return cached;
}
public object DeepCopy( object value )
{
return value;
}
public object Disassemble( object value )
{
return value;
}
public new bool Equals( object x, object y )
{
if(ReferenceEquals(x, y ) )
{
return true;
}
if( x == null || y == null )
{
return false;
}
return x.Equals( y );
}
public int GetHashCode( object x )
{
return x == null ? typeof( Color ).GetHashCode() + 473 : x.GetHashCode();
}
public bool IsMutable
{
get
{
return true;
}
}
public object NullSafeGet( IDataReader rs, string[] names, object owner )
{
var obj = NHibernateUtil.String.NullSafeGet( rs, names[0] );
if( obj == null )
{
return null;
}
return ColorTranslator.FromHtml( (string)obj );
}
public void NullSafeSet( IDbCommand cmd, object value, int index )
{
if( value == null )
{
( (IDataParameter)cmd.Parameters[index] ).Value = DBNull.Value;
}
else
{
( (IDataParameter)cmd.Parameters[index] ).Value = ColorTranslator.ToHtml( (Color)value );
}
}
public object Replace( object original, object target, object owner )
{
return original;
}
public Type ReturnedType
{
get
{
return typeof( Color );
}
}
public SqlType[] SqlTypes
{
get
{
return new[] { new SqlType( DbType.StringFixedLength ) };
}
}
}
ColorUserTypeTests.cs
[TestClass]
public class ColorUserTypeTests
{
public TestContext TestContext { get; set; }
[TestMethod]
public void AssembleTest()
{
var color = Color.Azure;
var userType = new ColorUserType();
var val = userType.Assemble( color, null );
Assert.AreEqual( color, val );
}
[TestMethod]
public void DeepCopyTest()
{
var color = Color.Azure;
var userType = new ColorUserType();
var val = userType.DeepCopy( color );
Assert.AreEqual( color, val );
}
[TestMethod]
public void DissasembleTest()
{
var color = Color.Azure;
var userType = new ColorUserType();
var val = userType.Disassemble( color );
Assert.AreEqual( color, val );
}
[TestMethod]
public void EqualsTest()
{
var color1 = Color.Azure;
var color2 = Color.Bisque;
var color3 = Color.Azure;
var userType = new ColorUserType();
var obj1 = (object)color1;
var obj2 = obj1;
Assert.IsFalse( userType.Equals( color1, color2 ) );
Assert.IsTrue( userType.Equals( color1, color1 ) );
Assert.IsTrue( userType.Equals( color1, color3 ) );
Assert.IsFalse( userType.Equals( color1, null ) );
Assert.IsFalse( userType.Equals( null, color1 ) );
Assert.IsTrue( userType.Equals( null, null ) );
Assert.IsTrue( userType.Equals( obj1, obj2 ) );
}
[TestMethod]
public void GetHashCodeTest()
{
var color = Color.Azure;
var userType = new ColorUserType();
Assert.AreEqual( color.GetHashCode(), userType.GetHashCode( color ) );
Assert.AreEqual( typeof( Color ).GetHashCode() + 473, userType.GetHashCode( null ) );
}
[TestMethod]
public void IsMutableTest()
{
var userType = new ColorUserType();
Assert.IsTrue( userType.IsMutable );
}
[TestMethod]
public void NullSafeGetTest()
{
var dataReaderMock = new Mock();
dataReaderMock.Setup( m => m.GetOrdinal( "white" ) ).Returns( 0 );
dataReaderMock.Setup( m => m.IsDBNull( 0 ) ).Returns( false );
dataReaderMock.Setup( m => m[0] ).Returns( "#ffffff" );
var userType = new ColorUserType();
var val = (Color)userType.NullSafeGet( dataReaderMock.Object, new[] { "white" }, null );
Assert.AreEqual( "ffffffff", val.Name, "The wrong color was returned." );
dataReaderMock.Setup( m => m.IsDBNull( It.IsAny() ) ).Returns( true );
Assert.IsNull( userType.NullSafeGet( dataReaderMock.Object, new[] { "black" }, null ), "The color was not null." );
dataReaderMock.VerifyAll();
}
[TestMethod]
public void NullSafeSetTest()
{
const string color = "#ffffff";
const int index = 0;
var mockFactory = new MockFactory( MockBehavior.Default );
var parameterMock = mockFactory.Create();
parameterMock.SetupProperty( p => p.Value, string.Empty );
var parameterCollectionMock = mockFactory.Create();
parameterCollectionMock.Setup( m => m[0] ).Returns( parameterMock.Object );
var commandMock = mockFactory.Create();
commandMock.Setup( m => m.Parameters ).Returns( parameterCollectionMock.Object );
var userType = new ColorUserType();
userType.NullSafeSet( commandMock.Object, ColorTranslator.FromHtml( color ), index );
Assert.AreEqual( 0, string.Compare( (string)( (IDataParameter)commandMock.Object.Parameters[0] ).Value, color, true ) );
userType.NullSafeSet( commandMock.Object, null, index );
Assert.AreEqual( DBNull.Value, ( (IDataParameter)commandMock.Object.Parameters[0] ).Value );
mockFactory.VerifyAll();
}
[TestMethod]
public void ReplaceTest()
{
var color = Color.Azure;
var userType = new ColorUserType();
Assert.AreEqual( color, userType.Replace( color, null, null ) );
}
[TestMethod]
public void ReturnedTypeTest()
{
var userType = new ColorUserType();
Assert.AreEqual( typeof( Color ), userType.ReturnedType );
}
[TestMethod]
public void SqlTypesTest()
{
var userType = new ColorUserType();
Assert.AreEqual( 1, userType.SqlTypes.Length );
Assert.AreEqual( new SqlType( DbType.StringFixedLength ), userType.SqlTypes[0] );
}
}