vendredi 19 juillet 2019

AutoFixture creates 2 Mock

I have the following test method:

[Theory, AutoData]
public void GetSerializer_MultipleSerializersAreRegistered_ProperSerializerIsReturned(
    Mock<IFileSerializer> mock1, Mock<IFileSerializer> mock2, Mock<IFileSerializer> mock3)
{
    _sut.AddFileSerializer(FileFormat.Csv, mock1.Object);
    _sut.AddFileSerializer(FileFormat.Json, mock2.Object);
    _sut.AddFileSerializer(FileFormat.Any, mock3.Object);

    var serializer = _sut.GetSerializer(FileFormat.Json);

    Assert.Equal(mock2.Object, serializer);
}

It throws exception:

Message: AutoFixture.ObjectCreationExceptionWithPath : AutoFixture was unable to create an instance from Moq.Mock`1[FileDataImporter.Services.Serialization.IFileSerializer] because creation unexpectedly failed with exception. Please refer to the inner exception to investigate the root cause of the failure.

Request path: Moq.Mock1[FileDataImporter.Services.Serialization.IFileSerializer] mock3
Moq.Mock
1[FileDataImporter.Services.Serialization.IFileSerializer]

Inner exception messages: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: value

---- System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation. -------- System.ArgumentOutOfRangeException : Specified argument was out of the range of valid values. Parameter name: value

However, when I remove mock3:

[Theory, AutoData]
public void GetSerializer_MultipleSerializersAreRegistered_ProperSerializerIsReturned(
    Mock<IFileSerializer> mock1, Mock<IFileSerializer> mock2)
{
    _sut.AddFileSerializer(FileFormat.Csv, mock1.Object);
    _sut.AddFileSerializer(FileFormat.Json, mock2.Object);

    var serializer = _sut.GetSerializer(FileFormat.Json);

    Assert.Equal(mock2.Object, serializer);
}

it works. What is the problem?

Aucun commentaire:

Enregistrer un commentaire