整套单元测试用例都通过了,但是如果我 运行 个人测试失败
Whole set of unit test cases pass but if I run individual it fails
下面是我的一组单元测试用例。不知道是不是我写错了。但是如果我 运行 一次全部通过(进入测试资源管理器--> 右键单击 CosmosDataTests--> 运行),我的所有单元测试用例都会通过。但是如果我尝试 运行 测试资源管理器中的单个测试用例,它会失败......说“所有者资源不可用”。我在这里做错了什么??
这是我的代码:
public class CosmosDataFixture : IDisposable
{
public static readonly string CosmosEndpoint = "https://localhost:8081";
public static readonly string EmulatorKey = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==";
public static readonly string DatabaseId = "dummydatabase";
public static readonly string RecordingCollection = "testcontainer";
public static string Root = Directory.GetParent( Directory.GetCurrentDirectory() ).Parent.Parent.FullName;
public static DocumentClient client { get; set; }
public async Task ReadAllData( DocumentClient client )
{
string path = Path.Combine( Root, @"TestData\Dummydata.json" );
var lines = File.ReadAllLines( path );
var jData = new List<JObject>();
foreach( var line in lines )
{
var data = JsonConvert.DeserializeObject<JObject>( line );
jData.Add( data );
}
Uri collectionUri = UriFactory.CreateDocumentCollectionUri( DatabaseId, RecordingCollection );
foreach( var obj in jData )
{
await client.UpsertDocumentAsync( collectionUri, obj );
}
}
public async Task ReadConfigAsync()
{
client = new DocumentClient( new Uri( CosmosEndpoint ), EmulatorKey,
new ConnectionPolicy
{
ConnectionMode = ConnectionMode.Direct,
ConnectionProtocol = Protocol.Tcp
} );
await client.CreateDatabaseIfNotExistsAsync( new Database { Id = DatabaseId } );
await client.CreateDocumentCollectionIfNotExistsAsync( UriFactory.CreateDatabaseUri( DatabaseId ),
new DocumentCollection { Id = RecordingCollection } );
await ReadAllData( client );
}
private async Task DeleteDatabaseFromPowerShell()
{
await client.DeleteDatabaseAsync( UriFactory.CreateDatabaseUri( DatabaseId ) );
}
public CosmosDataFixture()
{
ReadConfigAsync();
}
public void Dispose()
{
DeleteDatabaseFromPowerShell();
}
}
public class CosmosDataTests : IClassFixture<CosmosDataFixture>
{
[Fact]
public async Task CheckDatabaseandCollectionCreation()
{
List<string> collectionName = new List<string>();
var uri = UriFactory.CreateDatabaseUri( CosmosDataFixture.DatabaseId);
var collections = await CosmosDataFixture.client.ReadDocumentCollectionFeedAsync( uri );
foreach( var collection in collections )
{
collectionName.Add( collection.Id);
}
Assert.Contains( "testcontainer", collectionName );
}
[Fact]
public void AddDataInCosmosDbEmulator()
{ ...2nd test case..and so on}
详细错误消息:
Message:
System.Management.Automation.CmdletInvocationException : Message: {"Errors":["Resource Not Found. Learn more:
https://aka.ms/cosmosdb-tsg-not-found"]}
ActivityId: 1e276085-da33-42a0-8e45-9c5d0e951512, Request URI: /apps/DocDbApp/services/DocDbMaster0/partitions/780e44f4-38c8-11e6-8106-8cdcd42c33be/replicas/1p/,
RequestStats:
RequestStartTime: 2021-07-05T12:17:54.9135143Z, RequestEndTime: 2021-07-05T12:17:54.9135143Z, Number of regions attempted:1
, SDK: Microsoft.Azure.Documents.Common/2.11.0, Windows/10.0.19042 documentdb-netcore-sdk/2.14.0
---- Microsoft.Azure.Documents.DocumentClientException : Message: {"Errors":["Resource Not Found. Learn more:
https://aka.ms/cosmosdb-tsg-not-found"]}
ActivityId: 1e276085-da33-42a0-8e45-9c5d0e951512, Request URI: /apps/DocDbApp/services/DocDbMaster0/partitions/780e44f4-38c8-11e6-8106-8cdcd42c33be/replicas/1p/,
RequestStats:
RequestStartTime: 2021-07-05T12:17:54.9135143Z, RequestEndTime: 2021-07-05T12:17:54.9135143Z, Number of regions attempted:1
, SDK: Microsoft.Azure.Documents.Common/2.11.0, Windows/10.0.19042 documentdb-netcore-sdk/2.14.0 Stack Trace:
PipelineBase.Invoke(IEnumerable input)
Pipeline.Invoke()
CosmosDataTests.VerifyGetChangeFeedDocumentsForRecordings() line 177
----- Inner Stack Trace -----
GatewayStoreClient.ParseResponseAsync(HttpResponseMessage responseMessage, JsonSerializerSettings serializerSettings,
DocumentServiceRequest request)
GatewayStoreClient.InvokeAsync(DocumentServiceRequest request, ResourceType resourceType, Uri physicalAddress, CancellationToken
cancellationToken)
GatewayStoreModel.ProcessMessageAsync(DocumentServiceRequest request, CancellationToken cancellationToken)
DocumentClient.ProcessRequestAsync(DocumentServiceRequest request, IDocumentClientRetryPolicy retryPolicyInstance, CancellationToken
cancellationToken)
DocumentClient.ProcessRequestAsync(String verb, DocumentServiceRequest request, IDocumentClientRetryPolicy
retryPolicyInstance, CancellationToken cancellationToken, String
testAuthorization)
ChangeFeedQuery1.GetFeedResponseAsync(String resourceLink, ResourceType resourceType, IDocumentClientRetryPolicy retryPolicyInstance, CancellationToken cancellationToken) ChangeFeedQuery
1.ReadDocumentChangeFeedPrivateAsync[TResult](String
link, IDocumentClientRetryPolicy retryPolicyInstance,
CancellationToken cancellationToken)
BackoffRetryUtility1.ExecuteRetryAsync(Func
1 callbackMethod, Func3 callShouldRetry, Func
1 inBackoffAlternateCallbackMethod,
TimeSpan minBackoffForInBackoffCallback, CancellationToken
cancellationToken, Action1 preRetryCallback) ShouldRetryResult.ThrowIfDoneTrying(ExceptionDispatchInfo capturedException) BackoffRetryUtility
1.ExecuteRetryAsync(Func1 callbackMethod, Func
3 callShouldRetry, Func1 inBackoffAlternateCallbackMethod, TimeSpan minBackoffForInBackoffCallback, CancellationToken cancellationToken, Action
1 preRetryCallback)
CosmosDBDocumentStore.GetChangeFeedDocuments[T](String& ContinuationToken, TimeSpan MaxAge, Int32 MaxResults) line 94
projectName.GetChangeFeedDocuments[T](String& ContinuationToken, TimeSpan MaxAge, Int32 MaxResults) line 1037
GetProjectNameChangedRecordingsCmd.ProcessRecord() line 74
Cmdlet.DoProcessRecord()
CommandProcessor.ProcessRecord()
终于弄明白了,这是一个异步等待问题。在我的构造函数中调用并在 dispose 中调用的方法没有等待,这就是我出错的原因。通过添加 .GetAwaiter().Getresult()
修复它
public CosmosDataFixture()
{
ReadConfigAsync().GetAwaiter.GetResult();
}
public void Dispose()
{
DeleteDatabaseFromPowerShell().GetAwaiter.GetResult();
}
下面是我的一组单元测试用例。不知道是不是我写错了。但是如果我 运行 一次全部通过(进入测试资源管理器--> 右键单击 CosmosDataTests--> 运行),我的所有单元测试用例都会通过。但是如果我尝试 运行 测试资源管理器中的单个测试用例,它会失败......说“所有者资源不可用”。我在这里做错了什么??
这是我的代码:
public class CosmosDataFixture : IDisposable
{
public static readonly string CosmosEndpoint = "https://localhost:8081";
public static readonly string EmulatorKey = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==";
public static readonly string DatabaseId = "dummydatabase";
public static readonly string RecordingCollection = "testcontainer";
public static string Root = Directory.GetParent( Directory.GetCurrentDirectory() ).Parent.Parent.FullName;
public static DocumentClient client { get; set; }
public async Task ReadAllData( DocumentClient client )
{
string path = Path.Combine( Root, @"TestData\Dummydata.json" );
var lines = File.ReadAllLines( path );
var jData = new List<JObject>();
foreach( var line in lines )
{
var data = JsonConvert.DeserializeObject<JObject>( line );
jData.Add( data );
}
Uri collectionUri = UriFactory.CreateDocumentCollectionUri( DatabaseId, RecordingCollection );
foreach( var obj in jData )
{
await client.UpsertDocumentAsync( collectionUri, obj );
}
}
public async Task ReadConfigAsync()
{
client = new DocumentClient( new Uri( CosmosEndpoint ), EmulatorKey,
new ConnectionPolicy
{
ConnectionMode = ConnectionMode.Direct,
ConnectionProtocol = Protocol.Tcp
} );
await client.CreateDatabaseIfNotExistsAsync( new Database { Id = DatabaseId } );
await client.CreateDocumentCollectionIfNotExistsAsync( UriFactory.CreateDatabaseUri( DatabaseId ),
new DocumentCollection { Id = RecordingCollection } );
await ReadAllData( client );
}
private async Task DeleteDatabaseFromPowerShell()
{
await client.DeleteDatabaseAsync( UriFactory.CreateDatabaseUri( DatabaseId ) );
}
public CosmosDataFixture()
{
ReadConfigAsync();
}
public void Dispose()
{
DeleteDatabaseFromPowerShell();
}
}
public class CosmosDataTests : IClassFixture<CosmosDataFixture>
{
[Fact]
public async Task CheckDatabaseandCollectionCreation()
{
List<string> collectionName = new List<string>();
var uri = UriFactory.CreateDatabaseUri( CosmosDataFixture.DatabaseId);
var collections = await CosmosDataFixture.client.ReadDocumentCollectionFeedAsync( uri );
foreach( var collection in collections )
{
collectionName.Add( collection.Id);
}
Assert.Contains( "testcontainer", collectionName );
}
[Fact]
public void AddDataInCosmosDbEmulator()
{ ...2nd test case..and so on}
详细错误消息:
Message: System.Management.Automation.CmdletInvocationException : Message: {"Errors":["Resource Not Found. Learn more: https://aka.ms/cosmosdb-tsg-not-found"]} ActivityId: 1e276085-da33-42a0-8e45-9c5d0e951512, Request URI: /apps/DocDbApp/services/DocDbMaster0/partitions/780e44f4-38c8-11e6-8106-8cdcd42c33be/replicas/1p/, RequestStats: RequestStartTime: 2021-07-05T12:17:54.9135143Z, RequestEndTime: 2021-07-05T12:17:54.9135143Z, Number of regions attempted:1 , SDK: Microsoft.Azure.Documents.Common/2.11.0, Windows/10.0.19042 documentdb-netcore-sdk/2.14.0 ---- Microsoft.Azure.Documents.DocumentClientException : Message: {"Errors":["Resource Not Found. Learn more: https://aka.ms/cosmosdb-tsg-not-found"]} ActivityId: 1e276085-da33-42a0-8e45-9c5d0e951512, Request URI: /apps/DocDbApp/services/DocDbMaster0/partitions/780e44f4-38c8-11e6-8106-8cdcd42c33be/replicas/1p/, RequestStats: RequestStartTime: 2021-07-05T12:17:54.9135143Z, RequestEndTime: 2021-07-05T12:17:54.9135143Z, Number of regions attempted:1 , SDK: Microsoft.Azure.Documents.Common/2.11.0, Windows/10.0.19042 documentdb-netcore-sdk/2.14.0 Stack Trace: PipelineBase.Invoke(IEnumerable input) Pipeline.Invoke() CosmosDataTests.VerifyGetChangeFeedDocumentsForRecordings() line 177 ----- Inner Stack Trace ----- GatewayStoreClient.ParseResponseAsync(HttpResponseMessage responseMessage, JsonSerializerSettings serializerSettings, DocumentServiceRequest request) GatewayStoreClient.InvokeAsync(DocumentServiceRequest request, ResourceType resourceType, Uri physicalAddress, CancellationToken cancellationToken) GatewayStoreModel.ProcessMessageAsync(DocumentServiceRequest request, CancellationToken cancellationToken) DocumentClient.ProcessRequestAsync(DocumentServiceRequest request, IDocumentClientRetryPolicy retryPolicyInstance, CancellationToken cancellationToken) DocumentClient.ProcessRequestAsync(String verb, DocumentServiceRequest request, IDocumentClientRetryPolicy retryPolicyInstance, CancellationToken cancellationToken, String testAuthorization) ChangeFeedQuery
1.GetFeedResponseAsync(String resourceLink, ResourceType resourceType, IDocumentClientRetryPolicy retryPolicyInstance, CancellationToken cancellationToken) ChangeFeedQuery
1.ReadDocumentChangeFeedPrivateAsync[TResult](String link, IDocumentClientRetryPolicy retryPolicyInstance, CancellationToken cancellationToken) BackoffRetryUtility1.ExecuteRetryAsync(Func
1 callbackMethod, Func3 callShouldRetry, Func
1 inBackoffAlternateCallbackMethod, TimeSpan minBackoffForInBackoffCallback, CancellationToken cancellationToken, Action1 preRetryCallback) ShouldRetryResult.ThrowIfDoneTrying(ExceptionDispatchInfo capturedException) BackoffRetryUtility
1.ExecuteRetryAsync(Func1 callbackMethod, Func
3 callShouldRetry, Func1 inBackoffAlternateCallbackMethod, TimeSpan minBackoffForInBackoffCallback, CancellationToken cancellationToken, Action
1 preRetryCallback) CosmosDBDocumentStore.GetChangeFeedDocuments[T](String& ContinuationToken, TimeSpan MaxAge, Int32 MaxResults) line 94 projectName.GetChangeFeedDocuments[T](String& ContinuationToken, TimeSpan MaxAge, Int32 MaxResults) line 1037 GetProjectNameChangedRecordingsCmd.ProcessRecord() line 74 Cmdlet.DoProcessRecord() CommandProcessor.ProcessRecord()
终于弄明白了,这是一个异步等待问题。在我的构造函数中调用并在 dispose 中调用的方法没有等待,这就是我出错的原因。通过添加 .GetAwaiter().Getresult()
修复它public CosmosDataFixture()
{
ReadConfigAsync().GetAwaiter.GetResult();
}
public void Dispose()
{
DeleteDatabaseFromPowerShell().GetAwaiter.GetResult();
}