Search code examples
visual-studiomspec

How do I suppress or fix Visual Studio warnings that MSpec Behaves_like fields are unused?


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.


Solution

  • 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:

    #pragma warning disable 0169
    ... field declaration
    #pragma warning restore 0169