"..capturing the values at setup time" means caching them as lambdas,
instead of evaluating the real values.
"...I'm making heavy use of NUnit's new parameterised test features,
so I'm using variables in lots of places that others might use simplevalues..."
Can you elaborate more on this?
Currently I do this for almost every test:
1) Core mock setup - 5 or 6 mocks, approx 30-40 mock.Setup() calls.
2) Test-specific mock setup (typically 1 or 2 mock.Setup() calls)
3) Perform test
My idea is that instead of this I'd do the core mock setup once at the
start of the run, calling mock.SaveState() on each mock, and then each
test would become:
1) Reset core mock setup to default state, i.e. call
mock.RestoreState() on all mocks
2) Test-specific mock setup
3) Perform test
(Actually it's a little more complex than this because my core mock
setup is parameterised, so I'd need to manage a collection of core
mock setups, but the same basic principle applies)
An alternative would be to have a Clone() method on the mock - step 1
above would become "Clone default mocks". This might lend itself
better to parallelism of tests, because each test would be working on
its own separate mock object, rather than re-using the one from the
previous test.
I coded the Clone() method and it was straightforward to get it
working for simple Setup calls. (I didn't test anything else like
Verify() or recursive mocks)
So I think Clone() is probably the way to go. I guess we can wait
and see if many others get bitten by the performance issue that I
encountered - certainly doesn't seem like anyone else is complaining,
so maybe my scenario is quite unusual.
For the time being I'm happy to stick with my FastSetup workaround,
which has the advantage of not requiring any changes to Moq itself.