MStest 中的程序集上下文被更改 - 可以避免吗?

发布于 2024-08-04 17:58:54 字数 432 浏览 0 评论 0原文

我正在尝试在我们的代码库上使用 MSTest。现在我在此期间遇到了不同的问题。

我们隐式使用 Microsoft.Practices.EnterpriseLibrary.Data 中的 _getDefaultName 及其获取当前程序集 App.config 中的默认连接字符串。由于此测试项目将是一个新的程序集/项目,因此无法在源项目中找到连接字符串。

我可以通过对连接字符串进行硬编码或在源代码中显式给出 App.config 的路径来避免这种情况。

但我不想为了测试代码而更改源代码,那么有没有办法指定或更改当前运行的程序集?

如果我使用任何其他测试框架,我的生活会变得简单吗?

I am trying to use MSTest on our code base. Now I am running into different problems during this.

We implicitly use _getDefaultName from Microsoft.Practices.EnterpriseLibrary.Data and it gets the default connection string in the current Assembly's App.config. Since this test project would be a new assembly/project it is not able to find the connection string in the source project.

I can avoid this by hard coding the connection string or giving the path of the App.config explicitly in source code.

But I do not want to change the source code for the sake of test code, so is there a way to specify or change the current running assembly?

Would my life be simple if I use any other testing framework?

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

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

发布评论

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

评论(2

踏雪无痕 2024-08-11 17:58:54

好吧,如果您使用其他测试框架,您的生活可能会更简单;)(我使用并喜欢 NUnit)但这不太可能解决这个问题。

我建议将 App.config 数据复制到单元测试程序集中。您可能想要更改它,因为您可能不希望针对生产数据库运行测试。

如果您想要进行单元测试(而不是集成测试),请考虑编写根本不访问数据库的测试。这通常是通过使用分层架构、依赖注入和隔离(模拟对象)框架来完成的。 SO 上有很多关于这些单独主题的内容;如果您感兴趣但无法找到资源,请发表评论。

Well, your life might be simpler if you used other testing frameworks ;) (I use and like NUnit) but that's unlikely to resolve this issue.

I'd suggest just copying the App.config data into the unit test assembly. You may want to change it, as presumably you don't want your tests running against a production database.

If you want to do unit testing (as opposed to integration testing), consider writing tests that don't access the database at all. This is generally accomplished through the use of a layered architecture, dependency injection, and isolation (mock object) frameworks. There's a lot on these individual topics on SO; please post a comment if you're interested but are having trouble finding resources.

灯角 2024-08-11 17:58:54

您可以考虑另一篇文章中概述的方法。我采用了该链接中的示例之一,并将其​​更改为类似于您的情况:

    public static class Context
    {
        private static string ConnectString;

        public static string Connect
        {
            get { return ConnectString ?? DataFactory.instance; }
            set { ConnectString= value; }
        }

        private class DataFactory
        {
            static DataFactory() { }
            internal static readonly string instance
                   = EntLib.Data._getDefaultName;
        }
    } 

现在,当生产代码运行时,它从 EntLib 获取其配置 - 从 App.Config 文件。但是,当您运行 mstest 代码时,请确保在测试程序集的任何部分之前将所需的连接字符串注入到 Context.Connect 中。

You might consider an approach as outlined in another post. I took one of the examples in that link, and changed it to look like your case:

    public static class Context
    {
        private static string ConnectString;

        public static string Connect
        {
            get { return ConnectString ?? DataFactory.instance; }
            set { ConnectString= value; }
        }

        private class DataFactory
        {
            static DataFactory() { }
            internal static readonly string instance
                   = EntLib.Data._getDefaultName;
        }
    } 

Now when production code runs, it gets its configuration from EntLib - from the App.Config file. But when you run your mstest code, you make sure to inject the connection string you need into Context.Connect before any part of the assembly is tested.

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