如何在FLAUI/WPF窗口中注入模拟?

发布于 2025-01-22 14:47:25 字数 1218 浏览 0 评论 0原文

在我的WPF应用中,我有一个mainwindow.xaml和相关mainwindow.xaml.cs。后者有一个属性databaseaccessor访问数据库的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.core.automationelements.windowsystem.window.window

如果不可能,您将如何解决这样的问题?

In my WPF app, I have a MainWindow.xaml and a related MainWindow.xaml.cs. The latter has a property DatabaseAccessor which accesses a database:

public partial class MainWindow : System.Windows.Window
{
    public IDatabaseAccessor DatabaseAccessor { get; set; }

    public MainWindow()
    {
        InitializeComponent();
        DatabaseAccessor = new RealDatabaseAccessor();
    }

    // [...]
}

Now I'm writing a test using FlaUI. In my test I'm writing something like

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

For my tests, I do not want to communicate with the real database but mock it, instead.

How can I inject a mocked DatabaseAccessor into the Window opened with FlatUI (see the commented code in my last snippet)? Or in other words: How can I access the System.Windows.Window for a given FlaUI.Core.AutomationElements.Window?

If this is not possible, how would you approach such a problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰沐子 2025-01-29 14:47:25

您需要重新考虑您在这里做的事情。

UI 自动化测试不应该模拟依赖项。它在单独的过程中启动您的应用程序,并使用屏幕上显示的实际元素与之进行交互。

如果要在代码中测试特定组件,则应针对此组件编写单元测试。然后,您可以模拟任何外部依赖关系。

You need to rethink what you are doing here.

An UI automation test is not supposed to mock dependencies. It starts your app in a separate process and interacts with it using the actual elements that are presented on the screen.

If you want to test a specific component in your code, you should write a unit test against this component. You can then mock any external dependencies.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文