Closed jacob7395 closed 5 days ago
I probably should have read the docs first dose this no longer apply;
If methods are returning reference types, they should return a `Func<T>` rather than just a `T` - This ensures each test has its own instance of that object and tests aren't sharing objects which could lead to unintended side effects.
I tried the example bellow and everything is fine;
public class AdditionTestData
{
public int Value1 { get; }
public int Value2 { get; }
public int ExpectedResult { get; }
public AdditionTestData(int value1, int value2, int expectedResult)
{
Value1 = value1;
Value2 = value2;
ExpectedResult = expectedResult;
}
}
public static class MyTestDataSources
{
public static AdditionTestData AdditionTestData()
{
return new AdditionTestData(1, 2, 3);
}
}
public class MyTestClass
{
[Test]
[MethodDataSource(typeof(MyTestDataSources), nameof(MyTestDataSources.AdditionTestData))]
public async Task MyTest(AdditionTestData additionTestData)
{
var result = Add(additionTestData.Value1, additionTestData.Value2);
await Assert.That(result).IsEqualTo(additionTestData.ExpectedResult);
}
private int Add(int x, int y)
{
return x + y;
}
}
Are there some types this is still required for? and in those cases should your method take Func
public class AdditionTestData
{
public int Value1 { get; }
public int Value2 { get; }
public int ExpectedResult { get; }
public AdditionTestData(int value1, int value2, int expectedResult)
{
Value1 = value1;
Value2 = value2;
ExpectedResult = expectedResult;
}
}
public static class MyTestDataSources
{
public static IEnumerable<Func<AdditionTestData>> AdditionTestData()
{
yield return () => new AdditionTestData(1, 2, 3);
}
}
public class MyTestClass
{
[Test]
[MethodDataSource(typeof(MyTestDataSources), nameof(MyTestDataSources.AdditionTestData))]
public async Task MyTest(Func<AdditionTestData> testDataBuilder)
{
var additionTestData = testDataBuilder.Invoke();
var result = Add(additionTestData.Value1, additionTestData.Value2);
await Assert.That(result).IsEqualTo(additionTestData.ExpectedResult);
}
private int Add(int x, int y)
{
return x + y;
}
}
```.
Reading it again I guess this is to trying to prevent tests modifying a shared reference (just do immutability bro), in this case is the MethodDataSource
no longer supporting func<..>
a bug, if you try one of the example in the current docs it give's a build error.
in this case is the MethodDataSource no longer supporting func<..> a bug, if you try one of the example in the current docs it give's a build error.
Which example?
I am missing something, maybe I did encounter an issue since there are tests for this in TUnit.TestProject
and although I get errors before building it builds fine.
in this case is the MethodDataSource no longer supporting func<..> a bug, if you try one of the example in the current docs it give's a build error.
Which example?
When I copy any of the example into Rider I get the bellow error in my IDE, but they run and build fine?
Okay, I think the issue I was getting was due to my main project being out of date.
I was trying to replicate the issue in an example project when I get the above error that threw me off. Updating my package solved the build error on my main project.
I've found sometimes updating packed with analyzers requires restarting the ide to reload the analyzers with the new logic
Will close this as this wasn't an issue, the IDE error shown above goes away once the test is run. Maybe something to look into but I am guessing just something to do with how the tests are generated.
Update the docs for
docs/docs/tutorial-basics/method-data-source.md
to build/work by removing thefunc<..>
syntax.Maybe it's overkill but could have these example as part of the test suit, then use a build action to copy them into files referenced in the docs?
Not needed for this but could help prevent the issue in future.