[英]How to write unit tests for a Bot Framework v4 Dialog that requires the user to authenticate?
I'm writing Unit Tests (C#) for Dialogs created with version 4 of the MS Bot Framework. 我正在为使用MS Bot Framework第4版创建的Dialog编写单元测试(C#)。 I'm wondering how to unit test a ComponentDialog containing a WaterfallDialog and a OAuthPrompt where one step of the WaterfallDialog requires the user to login using the OAuthprompt?
我想知道如何对包含WaterfallDialog和OAuthPrompt的ComponentDialog进行单元测试,其中WaterfallDialog的一个步骤要求用户使用OAuthprompt登录?
When I write a Unit Test for such a Dialog using the TestAdapter and TestFlow I get to following exception: 当我使用TestAdapter和TestFlow为这样的Dialog编写单元测试时,我得到以下异常:
OAuthPrompt.GetUserToken(): not supported by the current adapter
This makes sense because the TestAdapter doesn't have a GetUserTokenAsync method like the BotFrameworkAdapter has (this method it called from OAuthPrompt.GetUserToken). 这是有道理的,因为TestAdapter没有像BotFrameworkAdapter那样的GetUserTokenAsync方法(这个方法是从OAuthPrompt.GetUserToken调用的)。
How can we work around this? 我们如何解决这个问题?
The blunt, honest answer is that this will be extremely difficult to factor properly for unit testing today without making a large investment in working around the limitations of the APIs today. 坦率,诚实的答案是,如果不对今天的API局限性进行大量投资,今天对于单元测试来说,这将是非常困难的。
As you pointed out in this specific case OAuthPrompt
has strong ties to the ITurnContext::Adapter
property being an instance of BotFrameworkAdapter
specifically. 当你在这种特殊情况下指出
OAuthPrompt
具有的紧密联系ITurnContext::Adapter
属性为实例BotFrameworkAdapter
明确。 This alone is "not good", but coupled with the fact that you can't actually override the APIs on BotFrameworkAdapter
that the OAuthPrompt
actually needs to call means you're completely stuck unless you use an advanced faking framework that allows for replacing non-virtual members. 仅此一点“不好”,但结合以下事实:您实际上无法覆盖
BotFrameworkAdapter
上的OAuthPrompt
实际需要调用的API意味着您完全陷入困境,除非您使用允许替换非的高级伪造框架虚拟成员。
Honestly, having looked into this for a bit, I don't think you can work around this right now. 老实说,考虑到这一点,我认为你现在不能解决这个问题。 I would implore you to raise this an issue in the
botbuilder-dotnet
repo on GitHub and I will gladly chime in there with a recommendation to the team on how they could go about fixing this. 我想恳请你在GitHub上的
botbuilder-dotnet
repo中提出这个问题,我很乐意在那里向团队提出建议,告诉他们如何解决这个问题。
UPDATE 8/15/2019 更新8/15/2019
Since the original answer, this was refactored to introduce an IUserTokenProvider
interface which the OAuthPrompt
now checks to see if the current ITurnContext::Adapter
supports and, if so, calls through to its implementation. 从最初的答案开始,这被重构为引入一个
IUserTokenProvider
接口, OAuthPrompt
现在检查该接口以查看当前的ITurnContext::Adapter
支持,如果支持,则调用其实现。 This means you can now mock this interface and implement the proper scenarios for tests. 这意味着您现在可以模拟此接口并实现适当的测试场景。
有一个很好的Channel 9系列可以解决这个问题,你可以在这里看到: https : //channel9.msdn.com/Series/DevOps-for-the-Bot-Framework/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.