Skip to content

Additional methods in fluent syntax causes exception 'variable referenced from scope ... but it is not defined' #251

Closed

Description

@develorem

System: Nunit test, resharper test runner, .Net 4.6, BDDfy version 4.1.2

Example code:

this.Given(x => x.GivenTheSiteCoverageService())
       .And(x => x.GivenTheFirstAerialJobSite().WithCoverage(80m).OverSite(FirstSite))
       ....

Effectively, putting the additional methods "WithCoverage" and "OverSite" causes BDDFY to fail with exception:

An exception of type 'System.InvalidOperationException' occurred in System.Core.dll but was not handled in user code

Additional information: variable 'x' of type '' referenced from scope '', but it is not defined

Removing the fluent extensions I created for my types allows it to work.
But the alternative of complex unreadable constructors for all my permutations is wrong (shouldn't have to change the object under test). Is there a way to achieve what I'm trying to achieve here?

Activity

  1. develorem commented on Jun 5, 2017

    @develorem
    Author

    Sorry, stack trace would have been helpful:

    at System.Linq.Expressions.Compiler.VariableBinder.Reference(ParameterExpression node, VariableStorageKind storage)
    at System.Linq.Expressions.Compiler.VariableBinder.VisitParameter(ParameterExpression node)
    at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node)
    at System.Linq.Expressions.Compiler.VariableBinder.VisitUnary(UnaryExpression node)
    at System.Linq.Expressions.ExpressionVisitor.Visit(ReadOnlyCollection1 nodes) at System.Linq.Expressions.Compiler.VariableBinder.VisitLambda[T](Expression1 node)
    at System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda, DebugInfoGenerator debugInfoGenerator)
    at TestStack.BDDfy.ExpressionExtensions.ArgumentExtractorVisitor.GetValue(Expression a)
    at TestStack.BDDfy.ExpressionExtensions.ArgumentExtractorVisitor.ExtractStepArgument(Expression a)
    at System.Linq.Enumerable.WhereSelectEnumerableIterator2.MoveNext() at System.Collections.Generic.List1.InsertRange(Int32 index, IEnumerable1 collection) at TestStack.BDDfy.ExpressionExtensions.ArgumentExtractorVisitor.VisitMethodCall(MethodCallExpression node) at System.Linq.Expressions.ExpressionVisitor.VisitLambda[T](Expression1 node)
    at TestStack.BDDfy.ExpressionExtensions.ExtractArguments[T](LambdaExpression expression, T value)
    at TestStack.BDDfy.FluentScanner1.AddStep(Action1 action, LambdaExpression stepAction, String stepTextTemplate, Boolean includeInputsInStepTitle, Boolean reports, ExecutionOrder executionOrder, Boolean asserts, String stepPrefix)
    at TestStack.BDDfy.FluentStepBuilder1.And(Expression1 step)

  2. develorem commented on Jun 5, 2017

    @develorem
    Author

    I suspect the offending code is the fact that the ExpressionExtensions.ArgumentExtractorVisitor.GetValue() method assumes a Func<object> when in fact the first call in my method chain returns an object as well.

    I added another method onto the chain that returned the current TScenario, and then called yet another method from TScenario (which did nothing) and it now works. Having these two extra methods is a bit lame. Also it sees that last method in the call chain as the one doing the work so it pulls name information from that one, rather than the first one (no problem on that, can specify the string to show).

  3. SonOfSardaar commented on May 30, 2026

    @SonOfSardaar
    Contributor

    this should be addressed by #307 . If it does not , then please re-open it with good example that can explain setup and expectation better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions