我有在控制器下面的方法(當用戶將機器人說歡迎消息)來發送從控制器本身的消息Conversations.SendToConversationAsync崩潰上單元測試
private static async Task<string> OnSendOneToOneMessage(Activity activity,
IList<Attachment> attachments = null)
{
var reply = activity.CreateReply();
if (attachments != null)
{
reply.Attachments = attachments;
}
if (_connectorClient == null)
{
_connectorClient = new ConnectorClient(new Uri(activity.ServiceUrl));
}
var resourceResponse = await _connectorClient.Conversations.SendToConversationAsync(reply);
return resourceResponse.Id;
}
和單元測試長相像這樣
[TestClass]
public sealed class MessagesControllerTest
{
[Test]
public async Task CheckOnContactRelationUpdate()
{
// Few more setup related to dB <deleted>
var activity = new Mock<Activity>(MockBehavior.Loose);
activity.Object.Id = activityMessageId;
activity.Object.Type = ActivityTypes.ContactRelationUpdate;
activity.Object.Action = ContactRelationUpdateActionTypes.Add;
activity.Object.From = new ChannelAccount(userId, userName);
activity.Object.Recipient = new ChannelAccount(AppConstants.BotId, AppConstants.BotName);
activity.Object.ServiceUrl = serviceUrl;
activity.Object.ChannelId = channelId;
activity.Object.Conversation = new ConversationAccount {Id = Guid.NewGuid().ToString()};
activity.Object.Attachments = Array.Empty<Attachment>();
activity.Object.Entities = Array.Empty<Entity>();
var messagesController =
new MessagesController(mongoDatabase.Object, null)
{
Request = new HttpRequestMessage(),
Configuration = new HttpConfiguration()
};
// Act
var response = await messagesController.Post(activity.Object);
var responseMessage = await response.Content.ReadAsStringAsync();
// Assert
Assert.IsNotEmpty(responseMessage);
}
}
當用戶添加bor時,OnSendOneToOneMessage方法工作正常。但它爲單元測試崩潰。似乎我錯過了POST的一些設置?
堆棧跟蹤爲
Result StackTrace:
at System.Net.Http.StringContent.GetContentByteArray(String content, Encoding encoding)
at System.Net.Http.StringContent..ctor(String content, Encoding encoding, String mediaType)
at System.Net.Http.StringContent..ctor(String content)
at <>.Controllers.MessagesController.<Post>d__4.MoveNext() in
C:\Users....MessagesController.cs:line 75
---在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務task) 從先前的位置堆棧跟蹤,其中引發異常--- 結束在系統.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務的任務) 在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 在BotTest.Controllers.MessagesControllerTest.d__0.MoveNext()在 C:\用戶.... MessagesControllerTest.cs:第75行 ---堆棧結束在NUnit.Framework.Internal.AsyncInvocationRegion.AsyncTaskInvocationRegion.WaitFor PendingOperationsToComplete(對象invocationResult) 在NUnit.Framework.Internal.Commands.TestMethodCommand.RunAsyncTestMethod(TestExecutionContext上下文) 結果消息從先前的位置,其中引發異常--- CE: System.ArgumentNullException:值不能爲空。 參數名:內容
這裏是輸出
Exception thrown: 'System.ArgumentNullException' in mscorlib.dll
Exception thrown: 'Microsoft.Rest.TransientFaultHandling.HttpRequestWithStatusException' in Microsoft.Rest.ClientRuntime.dll
Exception thrown: 'Microsoft.Rest.TransientFaultHandling.HttpRequestWithStatusException' in mscorlib.dll
Exception thrown: 'Microsoft.Rest.TransientFaultHandling.HttpRequestWithStatusException' in Microsoft.Rest.ClientRuntime.dll
Exception thrown: 'Microsoft.Rest.TransientFaultHandling.HttpRequestWithStatusException' in mscorlib.dll
Exception thrown: 'System.Net.Http.HttpRequestException' in System.Net.Http.dll
Exception thrown: 'System.UnauthorizedAccessException' in Microsoft.Bot.Connector.dll
Exception thrown: 'System.UnauthorizedAccessException' in mscorlib.dll
Exception thrown: 'System.UnauthorizedAccessException' in System.Net.Http.dll
Exception thrown: 'System.UnauthorizedAccessException' in mscorlib.dll
Exception thrown: 'System.UnauthorizedAccessException' in mscorlib.dll
注:我嘗試通過該憑證在所有不同的方式。它仍然在單元測試中崩潰。
但你在哪裏嘲諷連接器客戶端? –
我最初嘗試嘲笑,但這一個我正在嘗試從集成測試更多。因此,每次在OnSendOneToOneMessage方法中運行測試時都會創建連接器客戶端。 – dolbyarun
你使用的是什麼服務網址?您是否嘗試過使用[this構造函數](https://github.com/Microsoft/BotBuilder/blob/master/CSharp/Library/Microsoft.Bot.Connector.Shared/ConnectorAPI/ConnectorClient.cs#L207)對證書進行硬編碼?無論如何,我並不認爲這是進行集成測試的正確方法 –