如何抑制或修复 Visual Studio 警告 MSpec Behaves_like 字段未使用?
我正在使用 Behaviors
和 Behaves_like
字段编写惯用的 MSpec 规范
[Subject(typeof(IUnitMaskConverter))]
public class When_converting_unit_masks_by_lookup
{
Behaves_like<UnitMaskConverterBehaviors> a_unit_mask_converter;
protected static LookupUnitMaskConverter _converter = new LookupUnitMaskConverter();
}
Visual Studio 显示生成警告
The field 'Specs.UnitMask.When_converting_unit_masks_by_lookup.a_unit_mask_converter' is never used
我已经熟悉 MSpec 的 ReSharper 代码注释,并且我有 MSpec 主题的命名规则和领域。我不知道如何控制未使用字段的警告。我想避免在项目级别抑制警告,因为它在常规情况下实际上很有用。
I am writing idiomatic MSpec specs using Behaviors
and Behaves_like
fields
[Subject(typeof(IUnitMaskConverter))]
public class When_converting_unit_masks_by_lookup
{
Behaves_like<UnitMaskConverterBehaviors> a_unit_mask_converter;
protected static LookupUnitMaskConverter _converter = new LookupUnitMaskConverter();
}
Visual Studio displays a build warning
The field 'Specs.UnitMask.When_converting_unit_masks_by_lookup.a_unit_mask_converter' is never used
I am already familiar with the ReSharper code annotations for MSpec and I have naming rules for MSpec subjects and fields. I do not know how to control this warning for unused fields. I would like to avoid suppressing the warning at the project level, because it's actually useful in regular circumstances.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我知道这是一个近两年的问题;但是,看看原始问题的上下文,看起来 @Anthony 实际上想在他的测试项目中使用 MSpec。
@bitbonk 和 @mtijn 的两个答案给出了为什么您应该永远在项目级别忽略这些的充分理由。但是,这两个答案忽略了@Anthony的初衷,即他正在尝试使用MSpec。
您会看到,MSpec 是一个 BDD 框架,它使用大量委托和字段定义来定义您的规范。很多时候,您有未分配的字段和委托。这些会导致 VS 中的警告疯狂地飞来飞去,特别是如果您有 StyleCop 和其他自动化工具来控制开发人员。
想猜猜刚刚引起了多少警告?事实上,提前规范您的代码和项目是完全正常的。当 BDD 规范你的设计时,你不应该被一大堆警告惹恼:MSpec 的全部目的是在语法噪音最少的上下文中规范你的整体故事。警告,在你创建了十几个故事,每个故事有十几个规格之后,让它变得非常吵闹!
现在,我可以看到人们试图通过说“嘿,还没有测试,它只是被破坏了!警告表明我们需要实施它们”来证明警告的合理性。事实上,MSpec 在运行时已经在输出窗口中以不同的方式呈现了这些“存根”规格,在测试总计中标记为“跳过”,并且在 HTML 输出报告中也非常可爱。换句话说,您不需要警告来告诉您有未实现的规范,因为跑步者已经这样做了。
Behaves_like
已经有点奇怪了。但请注意,就是这样,不再有Behaves_like的实现了。行为
。它只是一个未分配的字段,MSpec 的运行程序使用(所有字段委托)并运行它们。因此,解决方案很简单:对于专门用于 Machine.Specifications 项目的 MSpec“Specs”测试项目,我经常右键单击项目的设置并将其添加到抑制框中:
0169;0649
同样,这仅适用于 MSpec 测试项目(或者任何其他 BDD C# 框架,因为它们使用大量委托和未分配的字段)。对于任何正常项目,您都不应该这样做。
或者,如果您的团队领导拒绝您为测试项目编辑它的权利,您可以继续实施您的规范,增加您首先试图通过使用 MSpec 来避免的同轴噪声(归咎于您的团队)导致“上下文切换”让你的生活变得更加困难!)。
这更加丑陋,并且确实让您偏离了您试图指定的整个 BDD 故事的焦点。更不用说,您的所有规范现在都将通过,但没有任何实现(您可以添加更多规范以使其成为硬失败,使用
抛出新的NotImplementedException()
- 但认真的吗?)。但是,如果您不想在项目级别忽略每个字段,那么这是一种“实现”每个字段的方法。I know this is nearly a two year old question; but, looking at the original question's context, it looks like @Anthony actually wants to use MSpec in his test projects.
The two answers by @bitbonk and @mtijn give great reasons of why you should never ignore these at the project level. But, these two answers ignored @Anthony's original intent, which is he is trying to use MSpec.
You see, MSpec is a BDD framework that uses heavy delegate and field definitions in order to define your specs. Often times, you have unassigned fields and delegates. These cause Warnings to fly like crazy in VS, especially if you have StyleCop and other automation tools to keep developers in check.
Want to take a guess how many warnings that just caused? When in fact, it's perfectly normal to spec our your code and projects well ahead of time. You shouldn't be annoyed by a butt-load of warnings when BDD-spec-ing your design: the whole point of MSpec is to spec your overall story in a context with the least amount of syntax noise. Warnings, make it extremely noisy after you create a dozen or so stories with a dozen or so specs each!
Now, I can see people trying to justify the warnings by saying, "Hey, there's no test yet, it is just stubbed! Warnings make it visible that we need to implement them." Well actually, MSpec already presents these "stubbed" specs in a different manner in the Output window when running, noted as "Skipped" in the test totals, and also quite lovely in their HTML output reports. In other words, you don't need Warnings to yell at you that there are specs unimplemented, cause the runners already do that.
The
Behaves_like<T>
is a little weird already. But notice that this is it, there's no more implementation toBehaves_like<T> behaviors
to that. It's just an unassigned field that MSpec's runner makes use of (all field delegates) and runs them.So the solution is simple though: For MSpec "Specs" test projects, dedicated solely to Machine.Specifications projects, I often right-click the project's settings and add these to the Supress box:
0169;0649
Again, this is only for MSpec test projects (or really any other BDD C# framework as they use heavy delegates and unassigned fields). You should never do this for any normal project.
Alternatively, if your team lead denies you that right to edit it for your test project, you can just go ahead and implement your specs, adding to the synaxial noise that you were trying to avoid in the first place by using MSpec (blame your team lead for making your life more difficult with "context-switching"!).
That is much more ugly, and really takes you off the focus of the overall BDD story you are trying to spec out. Not to mention, all of your specs will pass now with nothing implemented (you can add even more to make it a hard fail, with
throw new NotImplementedException()
- but seriously?). But, it's a way to "implement" each field if you don't want to ignore them at the project level.如果您使用 ReSharper,通常不需要用
#pragma warning disable 0169
噪音来扰乱您的代码,甚至更糟糕的是,在项目中全局禁用此警告。毕竟,MSpec 就是为了减少噪音和仪式。ReSharper 具有代码注释这一概念。 MSpec 为其类型提供了一些。如果您的 SubjectAttribute 超过对于您的类,ReSharper 会自动知道它不能抱怨未使用的字段。
不幸的是,ReSharper 6 中存在一个与此相关的错误,该错误已得到修复。
If you use ReSharper, usually, it is not necessary to clutter your code with
#pragma warning disable 0169
noise or even worse, globally disable this warning for the project. MSpec is all about less noise and ceremony after all.ReSharper has this concept of code annotations. And MSpec provides some for its types. In case you have the SubjectAttribute over your classes, ReSharper will automatically know that it must not complain about unused fields.
Unfortunately there is a bug about this in ReSharper 6 which has already been fixed.
如果警告有警告编号,您可以通过添加 编译指示禁用/启用。
要抑制“Field XYZ is neverused”的警告,您可以这个:
if the warnings have a warning number you could suppress those warnings per class or even line of code by adding pragma disable/enable.
To suppress warnings for "Field XYZ is never used", you do this: