使用Fakes进行单元测试会产生IConvertible异常

代码帮助

是)我有的

界面:

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

实现:

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());
}

异常发生在等待行中。

单元测试:

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);
}

该测试不会超出UpdateDocumentAsync部分,并退出并显示以下消息:

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

这是我第一次使用Fakes框架。任何帮助是极大的赞赏。

提前致谢。问候。

杰克·A

您的序列化代码似乎有问题。具体来说,此语句:

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

Value扩展方法似乎需要使源实现IConvertible接口,其不受实施Guid

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章