Skip to content

Commit 53854aa

Browse files
author
Oleksandr Poliakov
committed
Add unit test
1 parent 3dbc904 commit 53854aa

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

tests/MongoDB.Bson.Tests/Serialization/BsonClassMapSerializerTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,42 @@ public void Deserialize_should_throw_invalidOperationException_when_creator_retu
3939
exception.Should().BeOfType<BsonSerializationException>();
4040
}
4141

42+
[Fact]
43+
public void Deserialize_should_throw_when_no_creators_found()
44+
{
45+
var bsonClassMap = new BsonClassMap<ModelWithCtor>();
46+
bsonClassMap.AutoMap();
47+
bsonClassMap.Freeze();
48+
49+
var subject = new BsonClassMapSerializer<ModelWithCtor>(bsonClassMap);
50+
51+
using var reader = new JsonReader("{ \"_id\": \"just_an_id\" }");
52+
var context = BsonDeserializationContext.CreateRoot(reader);
53+
54+
var exception = Record.Exception(() => subject.Deserialize(context));
55+
exception.Should().BeOfType<BsonSerializationException>()
56+
.Subject.Message.Should().Be($"No matching creator found for class {typeof(ModelWithCtor).FullName}");
57+
}
58+
4259
// nested classes
4360
private class MyModel
4461
{
4562
public string Id { get; set; }
4663
}
64+
65+
private class ModelWithCtor
66+
{
67+
private readonly string _myId;
68+
private readonly int _i;
69+
70+
public ModelWithCtor(string id, int i)
71+
{
72+
_myId = id;
73+
_i = i;
74+
}
75+
76+
public string Id => _myId;
77+
public int I => _i;
78+
}
4779
}
4880
}

0 commit comments

Comments
 (0)