Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Repeat with automoq #1

Open
dilandau2001 opened this issue Oct 15, 2019 · 2 comments
Open

Repeat with automoq #1

dilandau2001 opened this issue Oct 15, 2019 · 2 comments

Comments

@dilandau2001
Copy link

dilandau2001 commented Oct 15, 2019

I was searching for a repeat attribute and I found yours very intereseting.

The thing is it only works for Fact tests.
I am using Autofixture with Moq and a AutomoqDataAttribute so all tests are magically Moq by the test. http://blog.nikosbaxevanis.com/2012/07/31/autofixture-xunit-net-and-auto-mocking/
The thing is, Repeat attribute does not care about the autofixture and only the first run fills the inline parameters.
I have tried to modify the code so it handles this but I was not able to do it.

I think that would be a very good upgrade.

@dilandau2001
Copy link
Author

dilandau2001 commented Oct 15, 2019

Finally I did it like this:

`

public sealed class RepeatAttribute : AutoDataAttribute
{
    private readonly int _count;

    /// <summary>
    /// Initializes a new instance of the <see cref="RepeatAttribute"/> class.
    /// </summary>
    /// <param name="count">Count number. Number of times to repeat the test.</param>
    public RepeatAttribute(int count) : this(count, new Fixture())
    {
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="RepeatAttribute"/> class.
    /// </summary>
    /// <param name="count">Count number. Number of times to repeat the test.</param>
    /// <param name="fixture">The fixture.</param>
    public RepeatAttribute(int count, IFixture fixture)
        : base(() => fixture
            .Customize(new AutoMoqCustomization { GenerateDelegates = true }))
    {
        const int minimumCount = 1;

        if (count < minimumCount)
        {
            throw new ArgumentOutOfRangeException(
                paramName: nameof(count),
                message: "Repeat count must be greater than 0."
            );
        }

        _count = count;
    }

    public override IEnumerable<object[]> GetData(MethodInfo testMethod)
    {
        foreach (var iterationNumber in Enumerable.Range(start: 1, count: _count))
        {
            var result = base.GetData(testMethod).ToArray();
            yield return result[0];
        }
    }
}

`

My only issue now is for frozen attributes, I have to take care in the test to reset them.

@MarcolinoPT
Copy link
Owner

Hi @dilandau2001, sorry for the long delay on my reply, could you provide a PR for this feature?

I am currently busy and will only have time to look at this mid November.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants