方法调用的所有引用的集成测试

发布于 2024-10-28 12:20:05 字数 1141 浏览 0 评论 0原文

所以,我在互联网上搜索了一下,试图看看是否有人已经在这里发明了轮子。我想要做的是编写一个集成测试,它将解析当前项目,查找对某个方法的所有引用,找到它的参数,然后检查数据库中的该参数。例如:

public interface IContentProvider
{
     ContentItem GetContentFor(string descriptor);
}

public class ContentProvider : IContentProvider
{
    public virtual ContentItem GetContentFor(string descriptor)
    {
        // Fetches Content from Database for descriptor and returns in
    }
}

任何其他类都会使用 IOC 将 IContentProvider 注入到其构造函数中,这样它们就可以编写如下内容:

contentProvider.GetContentFor("SomeDescriptor");
contentProvider.GetContentFor("SomeOtherDescriptor");

基本上,单元测试会找到所有这些引用,找到文本集 [“SomeDescriptor”,“SomeOtherDescriptor”],然后我可以检查数据库以确保为这些描述符定义了行。此外,描述符是硬编码的。

我可以为所有描述符创建一个枚举值,但是枚举将有数千种可能的选项,这看起来有点像黑客。

现在,这个链接如下: 我如何获得所有参考Reflection + C# 基本上表示,如果没有一些非常高级的 IL 解析,这是不可能的。澄清;我不需要 Reflector 或任何东西 - 它只是一个我可以运行的自动化测试,这样如果我团队中的任何其他开发人员签入调用此内容的代码而不创建数据库记录,测试就会失败。

这可能吗?如果是这样,有人有资源可以查看或示例代码可以修改吗?

编辑:或者,也许有一种不同的方法来执行此 VS 尝试查找所有引用?最终结果是我希望当记录不存在时测试失败。

So, I've been searching around on the internet for a bit, trying to see if someone has already invented the wheel here. What I want to do is write an integration test that will parse the current project, find all references to a certain method, find it's arguments, and then check the database for that argument. For example:

public interface IContentProvider
{
     ContentItem GetContentFor(string descriptor);
}

public class ContentProvider : IContentProvider
{
    public virtual ContentItem GetContentFor(string descriptor)
    {
        // Fetches Content from Database for descriptor and returns in
    }
}

Any other class will get an IContentProvider injected into their constructor using IOC, such that they could write something like:

contentProvider.GetContentFor("SomeDescriptor");
contentProvider.GetContentFor("SomeOtherDescriptor");

Basically, the unit test finds all these references, find the set of text ["SomeDescriptor", "SomeOtherDescriptor"], and then I can check the database to make sure I have rows defined for those descriptors. Furthermore, the descriptors are hard coded.

I could make an enum value for all descriptors, but the enum would have thousands of possible options, and that seems like kinda a hack.

Now, this link on SO: How I can get all reference with Reflection + C# basically says it's impossible without some very advanced IL parsing. To clarify; I don't need Reflector or anything - it's just to be an automated test I can run so that if any other developers on my team check in code that calls for this content without creating the DB record, the test will fail.

Is this possible? If so, does anyone have a resource to look at or sample code to modify?

EDIT: Alternatively, perhaps a different method of doing this VS trying to find all references? The end result is I want a test to fail when the record doesnt exist.

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

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

发布评论

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

评论(1

稍尽春風 2024-11-04 12:20:05

这将非常困难:您的程序可能会计算描述符的值,这意味着您的测试能够在不执行所述代码的情况下知道哪个值是可能的。

我建议更改您在此处编程的方式,使用 枚举类型,或使用类型安全枚举模式进行编码。这样,每次使用 GetContentFor 都将是安全的:参数是枚举的一部分,并且语言类型检查器执行检查。

然后,您的测试可以轻松地迭代不同的枚举字段,并检查它们是否都在您的数据库中声明,非常容易。

添加新的内容密钥需要编辑枚举,但这有点不方便,您可以忍受,因为它有助于记录确保所有调用都是安全的。

This will be very difficult: your program may compute the value of the descriptor, which will mean your test is able to know which value are possible without executing said code.

I would suggest to change the way you program here, by using an enum type, or coding using the type safe enum pattern. This way, each and every use of a GetContentFor will be safe: the argument is part of the enum, and the languages type checker performs the check.

Your test can then easily iterate on the different enum fields, and check they are all declared in your database, very easily.

Adding a new content key requires editing the enum, but this is a small inconvenient you can live with, as it help a log ensuring all calls are safe.

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