1

I am getting following exception:

System.ArgumentException: 'Only constant and single-dimensional array expressions are supported'

While trying to fake some abstact object with additional attribute that takes parameters in constructor.

var foo = A.Fake<SelfComplementaryCustomizableTupleConsumer>(
            opt => opt.WithAttributes(
                () => new RequiredVariableNameAttribute(requiredVariableName,requiredVariableType)
                )
            );

Worth mentioning is that if I invoke constructor with no arguments everything is just fine. What is even more instresting for me is that if I substitue variables with constants problem is not appearing either.

Complete code:

string requiredVariableName = "abc";
Type requiredVariableType = typeof(string);


var foo = A.Fake<SelfComplementaryCustomizableTupleConsumer>(
          opt => opt.WithAttributes(
                () => new RequiredVariableNameAttribute(requiredVariableName,requiredVariableType)
                )
         );
var requiredVariables = foo.GetRequiredVariables();

Assert.IsTrue(requiredVariables.TryGetValue(requiredVariableName, out Type tmp));
zajer
  • 649
  • 6
  • 17

1 Answers1

3

This is due to how the attribute creation expression is analyzed. It doesn't support all possible expressions, because it would be very complex. I guess it would be possible to handle the case of local variables, but the code is in Castle.Core (which is used by FakeItEasy), so don't expect a fix very soon. In the meantime, use constants in the expression if you can; if not, an alternative is to build the expression manually (using Expression.Lambda<Func<Attribute>>(...)).

EDIT: I opened an issue in the Castle.Core repo.

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758