241

An expression tree may not contain a call or invocation that uses optional arguments

return this.RedirectToAction<MerchantController>(x => x.Edit(merchantId));

Where edit had a second, nullable argument.

Why is this?

bevacqua
  • 47,502
  • 56
  • 171
  • 285

6 Answers6

286

Had the same message when trying to use Mock.setup to mock a method with multiple default parameters. I just had to add the additional parameters in the lambda.

void someMethod(string arg1 = "", string arg2 = "")

mockedObject.Setup(x => x.someMethod(It.IsAny<string>(), It.IsAny<string>()))
ds4940
  • 3,382
  • 1
  • 13
  • 20
154

The underlying expression tree API does not support optional arguments.

For IL-compiled code the C# compiler inserts the default values at compile time (hard-coded), because the CLR does not support calling methods with optional arguments either when the arguments are not provided explicitly.

usr
  • 168,620
  • 35
  • 240
  • 369
  • 2
    Does this somehow include overloads? I was getting this when I made an overload. Something like void Blah(string a) and void Blah(object a). When I tried to MOQ out a call to the version with object, it gave me this error. – vbullinger Mar 21 '13 at 14:02
  • 6
    Overloads are fully supported in the sense that a particular overload will be hard-coded into the tree. – usr Mar 21 '13 at 14:26
  • 1
    Pretty cryptic error message, but this answer showed the way, I had optional parameter with default value on the method is was trying to mock. – vpalmu Mar 24 '16 at 18:38
4

Error: 'an exception tree may not contain a call or invocation that uses option arguments'

Why: Because you are not providing the optional parameters when calling the method. Mainly you get this with .net core when using IAsyncProxy service object.

Fix: Pass all the optional parameters value, you may use default value if you.

Rakesh
  • 41
  • 2
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 16 '21 at 15:00
3

You might want to test that a method that has default parameters is called without any argument passed, in that case:

myMock.someMethod(default,default)

can work

Ced
  • 15,847
  • 14
  • 87
  • 146
1

I dealt with it by adding the optional parameter with a value . It worked. The same thing happened when I tried mocking while doing a setup .

Shumail
  • 65
  • 1
  • 6
0

I was getting this error with this:

var values = from ac in _dbContext.Entities<AuthCompany>()
                                 from uc in _dbContext.Entities<UserCompany>()
                                            .Where(x => x.CompanyID == ac.CompanyID).DefaultIfEmpty()
                                 select ac;

I fixed it by extracting the DbQuerys into their own variables:

var authCompanyEntities = _dbContext.Entities<AuthCompany>();
var userCompanyEntities = _dbContext.Entities<UserCompany>();

var values = from ac in authCompanyEntities
             from uc in userCompanyEntities.Where(x => x.CompanyID == ac.CompanyID).DefaultIfEmpty()
             select ac;
David Klempfner
  • 8,700
  • 20
  • 73
  • 153