在我的WPF应用程序中,我有一个MainWindow.xaml和一个相关的MainWindow.xaml.cs。后者具有一个属性DatabaseAccessor,该属性访问数据库:
public partial class MainWindow : System.Windows.Window
{
public IDatabaseAccessor DatabaseAccessor { get; set; }
public MainWindow()
{
InitializeComponent();
DatabaseAccessor = new RealDatabaseAccessor();
}
// [...]
}现在,我正在使用FlaUI编写一个测试。在我的测试中,我写了这样的东西
using FlaUI.UIA3;
using Moq;
var app = FlaUI.Core.Application.Launch("myapp.exe");
using (var automation = new UIA3Automation())
{
FlaUI.Core.AutomationElements.Window window = app.GetMainWindow(automation);
//// I am looking for a way to do something like this (but no such API exists):
// MainWindow myWindow = (MainWindow) window.RealWindow;
// myWindow.DatabaseAccessor = new Mock<IDatabaseAccessor>().Object;
}对于我的测试,我不想与真正的数据库通信,而是模拟它。
如何将模拟的DatabaseAccessor注入用FlatUI打开的窗口(请参阅我上一个片段中的注释代码)?或者换句话说:如何访问给定FlaUI.Core.AutomationElements.Window的FlaUI.Core.AutomationElements.Window
如果这是不可能的,你会如何处理这样的问题?
发布于 2022-04-21 13:48:19
你需要重新考虑你在这里做什么。
UI自动化测试不应该模拟依赖项。它以单独的进程启动应用程序,并使用屏幕上显示的实际元素与应用程序进行交互。
如果要测试代码中的特定组件,则应针对此组件编写单元测试。然后,您可以模拟任何外部依赖项。
https://stackoverflow.com/questions/71938050
复制相似问题