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

Example table variable conflicts with method parameters #282

Open
MichaelHancock opened this issue Feb 3, 2022 · 0 comments
Open

Example table variable conflicts with method parameters #282

MichaelHancock opened this issue Feb 3, 2022 · 0 comments

Comments

@MichaelHancock
Copy link

I ran into an exception when trying to write a test using the 4.3.2 version of the library. The exception was from linq but originating in the library code

System.InvalidOperationException : Sequence contains more than one matching element
Stack Trace:
at System.Linq.ThrowHelper.ThrowMoreThanOneMatchException()

Here is an example of how to trigger it

namespace TestStack.BDDfy.Testing
{
    using FluentAssertions;
    using NUnit.Framework;
    using TestStack.BDDfy;

    public class TestClass
    {
        private int myNumber;

        [Test]
        public void ContrivedTestExample()
        {
            var number = default(int);
            var expectedNumber = default(int);

            this.Given(_ => _.IHaveANumber(number))
                .When(_ => _.TheNumberIsIncremented())
                .Then(_ => _.TheNumberShouldBe(expectedNumber))
                .WithExamples(new ExampleTable("number", "expectedNumber")
                {
                    { 1, 2 },
                    { 2, 3 }
                })
                .BDDfy();
        }

        private void IHaveANumber(int inputNumber)
        {
            this.myNumber = inputNumber;
        }

        private void TheNumberIsIncremented()
        {
            this.myNumber++;
        }

        private void TheNumberShouldBe(int number)
        {
            this.myNumber.Should().Be(number);
        }
    }
}

The exception seems to be caused by naming a method parameter in the given when then chain the same as one of the example variables in the test. So the parameter in the TheNumberShouldBe method is number. There is also a example table variable for number.

Obviously, this a forced example but I ran into this unintentionally by just naming variables and parameters what I thought made sense. Renaming it to anything other than number fixes the test. Including renaming it to expectedNumber.

Now that I know what the cause is it is easy enough to fix and avoid but it took a while and a lot of hacking around to find an answer. The way the library determines the variable mappings might mean this cannot be fixed but perhaps could the library throw a more descriptive exception? Potentially including the name of the conflicting variable to make it easy for people to discover the fix?

@MichaelHancock MichaelHancock changed the title Undescriptive 'more than one match exception' Example table variable conflicts with method parameters Feb 3, 2022
MichaelHancock added a commit to MichaelHancock/TestStack.BDDfy that referenced this issue Mar 25, 2022
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

1 participant