@@ -100,17 +100,23 @@ public void RegisterSerializer_should_throw_when_serializer_is_already_registere
100100 [ Fact ]
101101 public void RegisterSerializer_should_throw_when_type_and_serializer_do_not_match ( )
102102 {
103+ var subject = new BsonSerializerRegistry ( ) ;
103104 var intSerializer = new Int32Serializer ( ) ;
104- var exception = Record . Exception ( ( ) => BsonSerializer . RegisterSerializer ( typeof ( long ) , intSerializer ) ) ;
105+
106+ var exception = Record . Exception ( ( ) => subject . RegisterSerializer ( typeof ( long ) , intSerializer ) ) ;
107+
105108 exception . Should ( ) . BeOfType < BsonSerializationException > ( ) ;
106- exception . Message . Should ( ) . Contain ( $ "A serializer for Int32 cannot be registered for type Int64.") ;
109+ exception . Message . Should ( ) . Contain ( "A serializer for Int32 cannot be registered for type Int64." ) ;
107110 }
108111
109112 [ Fact ]
110113 public void TryRegisterSerializer_should_throw_when_type_and_serializer_do_not_match ( )
111114 {
115+ var subject = new BsonSerializerRegistry ( ) ;
112116 var intSerializer = new Int32Serializer ( ) ;
113- var tryException = Record . Exception ( ( ) => BsonSerializer . TryRegisterSerializer ( typeof ( long ) , intSerializer ) ) ;
117+
118+ var tryException = Record . Exception ( ( ) => subject . TryRegisterSerializer ( typeof ( long ) , intSerializer ) ) ;
119+
114120 tryException . Should ( ) . BeOfType < BsonSerializationException > ( ) ;
115121 tryException . Message . Should ( ) . Contain ( "A serializer for Int32 cannot be registered for type Int64." ) ;
116122 }
@@ -124,16 +130,7 @@ public void RegisterSerializer_should_allow_serializer_for_base_type()
124130 var exception = Record . Exception ( ( ) => subject . RegisterSerializer ( typeof ( List < Person > ) , serializer ) ) ;
125131 exception . Should ( ) . BeNull ( ) ;
126132
127- var people = new List < Person >
128- {
129- new Person { Name = "Alice" } ,
130- new Person { Name = "Bob" }
131- } ;
132-
133- var json = people . ToJson < IEnumerable < Person > > ( ) ;
134- json . Should ( ) . Contain ( "Alice" ) ;
135- json . Should ( ) . Contain ( "Bob" ) ;
136-
133+ subject . GetSerializer ( typeof ( List < Person > ) ) . Should ( ) . BeSameAs ( serializer ) ;
137134 }
138135
139136 [ Fact ]
@@ -142,19 +139,10 @@ public void TryRegisterSerializer_should_allow_serializer_for_base_type()
142139 var subject = new BsonSerializerRegistry ( ) ;
143140 var serializer = new PeopleSerializer ( ) ;
144141
145- var exception = Record . Exception ( ( ) => subject . TryRegisterSerializer ( typeof ( List < Person > ) , serializer ) ) ;
146- exception . Should ( ) . BeNull ( ) ;
147-
148- var people = new List < Person >
149- {
150- new Person { Name = "Alice" } ,
151- new Person { Name = "Bob" }
152- } ;
153-
154- var json = people . ToJson < IEnumerable < Person > > ( ) ;
155- json . Should ( ) . Contain ( "Alice" ) ;
156- json . Should ( ) . Contain ( "Bob" ) ;
142+ var result = subject . TryRegisterSerializer ( typeof ( List < Person > ) , serializer ) ;
143+ result . Should ( ) . BeTrue ( ) ;
157144
145+ subject . GetSerializer ( typeof ( List < Person > ) ) . Should ( ) . BeSameAs ( serializer ) ;
158146 }
159147
160148 private class Person
@@ -169,16 +157,16 @@ public override void Serialize(BsonSerializationContext context, BsonSerializati
169157 context . Writer . WriteStartArray ( ) ;
170158 foreach ( var person in value )
171159 {
172- context . Writer . WriteStartDocument ( ) ;
173- context . Writer . WriteName ( "Name" ) ;
174- context . Writer . WriteString ( person . Name ) ;
175- context . Writer . WriteEndDocument ( ) ;
160+ context . Writer . WriteStartDocument ( ) ;
161+ context . Writer . WriteName ( "Name" ) ;
162+ context . Writer . WriteString ( person . Name ) ;
163+ context . Writer . WriteEndDocument ( ) ;
176164 }
177165 context . Writer . WriteEndArray ( ) ;
178166 }
179167
180- public override IEnumerable < Person > Deserialize ( BsonDeserializationContext context , BsonDeserializationArgs args )
181- => throw new NotImplementedException ( ) ;
168+ public override IEnumerable < Person > Deserialize ( BsonDeserializationContext context , BsonDeserializationArgs args )
169+ => throw new NotImplementedException ( ) ;
182170 }
183171
184172 [ Fact ]
0 commit comments