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

Setup with interpolated strings #821

Closed
Llujoo opened this issue Apr 30, 2019 · 4 comments
Closed

Setup with interpolated strings #821

Llujoo opened this issue Apr 30, 2019 · 4 comments

Comments

@Llujoo
Copy link

Llujoo commented Apr 30, 2019

The following code does not work
moq.Setup(m => m.Get("something[{i}].Name")).Returns($"foo {i}");
I had to use this instead.
moq.Setup(m => m.Get("something[" + i + "].Name")).Returns($"foo {i}");

@stakx
Copy link
Contributor

stakx commented Apr 30, 2019

Are you sure you didn't just miss a $ (see above, you have a regular "..." string instead of $"..." so obviously interpolation wouldn't happen).

@stakx
Copy link
Contributor

stakx commented Apr 30, 2019

The above being said, yes, it seems entirely likely that the workaround might still be needed since you're in an expression tree context. Generally speaking, you shouldn't be including computations in setup expressions that aren't necessary / strictly relevant for that setup.

@stakx stakx closed this as completed Apr 30, 2019
@stakx
Copy link
Contributor

stakx commented May 1, 2019

Yes, m.Get($"something[{i}].Name") is really equivalent to m.Get(string.Format("something[{0}].Name", i)). Unlike m.Get("something[" + i + "].Name"), the former will be lazily evaluated because it involves a function call as the argument. While using $"..." string interpolation should be supported, it'll lead to lazy evaluation, so if i is an iteration variable, for example, by the time your setups get invoked, i will have the final iteration value and that's what the setup(s) will use.

@Llujoo
Copy link
Author

Llujoo commented May 2, 2019

Interesting. Thank you for your response.

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