Search code examples
microsoft-fakesshim

Unit testing using Fakes gives IConvertible exception


What I have

The interface:

public interface IDocDbRepository
{
    Task<JObject> UpdateDocumentAsync(JObject updatedDocument);
}

The implementation:

public async Task<JObject> UpdateDocumentAsync(JObject updatedDocument)
{
    if (updatedDocument == null)
    {
        throw new ArgumentNullException(nameof(updatedDocument));
    }

    var response = await this.documentDBClient.ReplaceDocumentAsync(UriFactory.CreateDocumentUri(this.dbName, this.collectionName, updatedDocument["id"].Value<string>()), updatedDocument).ConfigureAwait(false);
    return JObject.Parse(response.Resource.ToString());
}

The exception occurs in the await line.

The Unit Test:

static Guid docGuid = Guid.NewGuid();

[TestMethod]
public async Task TestMethod2()
{
    var jObject = new JObject { { "id", docGuid }, { "studentId", "1" }, { "courseId", "Ph" } };

    // Arrange
    var docClient = new ShimDocumentClient();
    ShimDocumentClient.AllInstances.CreateDocumentAsyncUriObjectRequestOptionsBoolean =
       (instance, uri, document, options, disableAutomaticGeneration) => Task.FromResult(new ResourceResponse<Document>(new Document() { Id = docGuid.ToString() }));

    // Act
    var documentRepository = new DocDbRepository(endPointUri, accountKey, dbName, collectionName);
    try{
    var response = await documentRepository.UpdateDocumentAsync(jObject).ConfigureAwait(false);
    }
    catch(Exception ex){}

    // Assert
    Assert.AreEqual(response.Count, 1);
}

The test does not go beyond the UpdateDocumentAsync part and exits with this message:

at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
at Newtonsoft.Json.Linq.Extensions.Convert[T,U](T token)
at Newtonsoft.Json.Linq.Extensions.Value[T,U](IEnumerable`1 value)
at Newtonsoft.Json.Linq.Extensions.Value[U](IEnumerable`1 value)
at Common.DataAccess.DocumentDb.DocDbRepository.<UpdateDocumentAsync>d__12.MoveNext() in C:\Common\Common.DataAccess.DocumentDb\DocDbRepository.cs:line 196
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
at Common.DataAccess.DocumentDb.Tests.DocDbUtilityTests.<TestMethod2>d__9.MoveNext() in C:\Common\Common.DataAccess.DocumentDb.Tests\DocDbUtilityTests.cs:line 113

This is my first time with Fakes framework. Any help is greatly appreciated.

Thanks in advance. Regards.


Solution

  • This appears to be a problem with your serialization code. Specifically, this statement:

    updatedDocument["id"].Value<string>()
    

    The Value extension method appears to require that the source implement the IConvertible interface, which is not implemented by Guid.