Skip to content

[Bug]: IndexOutOfRangeException when using MethodDataSource on a class #5118

Description

@PandrPi

Description

Hello!
I hope you are doing well, guys!

I tried to apply the MethodDataSource attribute to a class so I can run async code to create a bunch of test cases for all the tests in that class. However, I cannot make it work without errors; the code compiles, but it throws an exception at runtime.

Expected Behavior

Hard to say, I only expect that the given code sample works well, or that I am provided with a modified version of that sample, that functions pretty much the same.

Actual Behavior

The exception is thrown at runtime

Steps to Reproduce

Here is the code sample that allows me to constantly reproduce the exception. It's basically a slightly modifi

public class AsyncTestDataSources
{
    public static async IAsyncEnumerable<(int Id, string Name, DateTime CreatedAt)> GetAsyncTestData()
    {
        for (var i = 1; i <= 3; i++)
        {
            await Task.Delay(10).ConfigureAwait(false);

            yield return (Id: i, Name: $"Item_{i}", CreatedAt: DateTime.UtcNow.AddDays(-i));
        }
    }
}

[MethodDataSource<AsyncTestDataSources>(nameof(AsyncTestDataSources.GetAsyncTestData))]
public class AsyncDataSourceTests(int Id, string Name, DateTime CreatedAt)
{
    [Test, Timeout(1000)]
    public async Task TestWithAsyncComplexData(CancellationToken token)
    {
        await Assert.That(Id).IsGreaterThan(0);
        await Assert.That(Name).StartsWith("Item_");
        await Assert.That(CreatedAt).IsLessThan(DateTime.UtcNow);
    }
}

TUnit Version

1.19.22

.NET Version

.NET 8.0

Operating System

Windows

IDE / Test Runner

dotnet CLI (dotnet test / dotnet run)

Error Output / Stack Trace

System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at TUnit.Generated.MyTests_Tests_AsyncDataSourceTests__TestSource.CreateInstance(Type[] typeArgs, Object[] args) in C:\Users\*****\****\TUnit.Core.SourceGenerator\TUnit.Core.SourceGenerator.Generators.TestMetadataGenerator\MyTests_Tests_AsyncDataSourceTests__TestSource.g.cs:line 131
   at TUnit.Core.TestMetadata`1.<>c__DisplayClass5_0.<set_InstanceFactory>b__0(Type[] typeArgs, Object[] args)
   at TUnit.Engine.Building.TestBuilder.CreateInstance(TestMetadata metadata, Type[] resolvedClassGenericArgs, Object[] classData, TestBuilderContext builderContext)
   at TUnit.Core.TestMetadata`1.<>c__DisplayClass14_0.<<get_CreateExecutableTestFactory>b__1>d.MoveNext()
--- End of stack trace from previous location ---
   at TUnit.Core.ExecutableTest.CreateInstanceAsync()
   at TUnit.Engine.Services.TestExecution.TestCoordinator.ExecuteTestLifecycleAsync(AbstractExecutableTest test, CancellationToken cancellationToken)
   at TUnit.Engine.Services.TestExecution.TestCoordinator.<>c__DisplayClass11_0.<<ExecuteTestInternalAsync>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at TUnit.Engine.Services.TestExecution.RetryHelper.ExecuteWithRetry(TestContext testContext, Func`1 action)
   at TUnit.Engine.Services.TestExecution.RetryHelper.ExecuteWithRetry(TestContext testContext, Func`1 action)
   at TUnit.Engine.Services.TestExecution.TestCoordinator.ExecuteTestInternalAsync(AbstractExecutableTest test, CancellationToken cancellationToken)


Here is how the generated code looks at the place the exception is thrown:

    internal static global::MyTests.AsyncDataSourceTests CreateInstance(global::System.Type[] typeArgs, object?[] args)
    {
        return new global::MyTests.AsyncDataSourceTests(global::TUnit.Core.Helpers.CastHelper.Cast<int>(args[0]), global::TUnit.Core.Helpers.CastHelper.Cast<string>(args[1]), global::TUnit.Core.Helpers.CastHelper.Cast<global::System.DateTime>(args[2]));
    }

Additional Context

No response

IDE-Specific Issue?

  • I've confirmed this issue occurs when running via dotnet test or dotnet run, not just in my IDE

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions