Search Results for

    Show / Hide Table of Contents

    NUnit1024

    The source specified by the ValueSource does not return an I(Async)Enumerable or a type that implements I(Async)Enumerable

    Topic Value
    Id NUnit1024
    Severity Error
    Enabled True
    Category Structure
    Code ValueSourceUsageAnalyzer

    Description

    The source specified by the ValueSource must return an I(Async)Enumerable or a type that implements I(Async)Enumerable.

    Motivation

    To prevent tests that will fail at runtime due to improper construction.

    How to fix violations

    Example Violation

    public class AnalyzeWhenSourceDoesProvideIEnumerable
    {
        private static readonly int testCases = 42;
    
        [Test]
        public void Test([ValueSource(nameof(testCases))] int input)
        {
        }
    }
    

    Explanation

    In the sample above, the source specified by ValueSource - the field testCases - does not return an I(Async)Enumerable or a type that implements I(Async)Enumerable, instead it returns an int.

    However, sources specified by ValueSource must return an I(Async)Enumerable or a type that implements I(Async)Enumerable..

    Fix

    Change testCases to return an I(Async)Enumerable or a type that implements I(Async)Enumerable:

    public class AnalyzeWhenSourceDoesProvideIEnumerable
    {
        private static readonly int[] testCases = new int[] { 1, 2, 42 };
    
        [Test]
        public void Test([ValueSource(nameof(testCases))] int input)
        {
        }
    }
    

    Configure severity

    Via ruleset file

    Configure the severity per project, for more info see MSDN.

    Via .editorconfig file

    # NUnit1024: The source specified by the ValueSource does not return an I(Async)Enumerable or a type that implements I(Async)Enumerable
    dotnet_diagnostic.NUnit1024.severity = chosenSeverity
    

    where chosenSeverity can be one of none, silent, suggestion, warning, or error.

    Via #pragma directive

    #pragma warning disable NUnit1024 // The source specified by the ValueSource does not return an I(Async)Enumerable or a type that implements I(Async)Enumerable
    Code violating the rule here
    #pragma warning restore NUnit1024 // The source specified by the ValueSource does not return an I(Async)Enumerable or a type that implements I(Async)Enumerable
    

    Or put this at the top of the file to disable all instances.

    #pragma warning disable NUnit1024 // The source specified by the ValueSource does not return an I(Async)Enumerable or a type that implements I(Async)Enumerable
    

    Via attribute [SuppressMessage]

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Structure",
        "NUnit1024:The source specified by the ValueSource does not return an I(Async)Enumerable or a type that implements I(Async)Enumerable",
        Justification = "Reason...")]
    
    • Edit this page
    In this article
    Back to top Generated by DocFX | Copyright (c) 2018- The NUnit Project - Licensed under CC BY-NC-SA 4.0