Instance invoked blocks for ActionDispatch::Integration::Runner#open_session

Currently the way open_session works requires you to change your test code to call everything on the passed in session in the block. I found this to be confusing and the documentation around how this work was a bit thin.

I ended up overriding open_session in our project so that it allowed for a block that takes no arguments and calls it as an instance_eval’d block on the session. I’ve linked to a gist with the code below and how it works.

I think this helps improve the consistency of the test code inside and outside of open_session blocks which was a sticking point for me while converting our old controller tests.

https://gist.github.com/chrisnicola/597725f548287f7e6f129567e8ab6f62

I’d be curious what people think of this approach. It also maintains the option of supplying an argument in the block and having it work the old way. This is similar to how the Sequel gem does blocks with virtual rows.

It seems reasonable, more readable in the way you propose.

So ignore this proposal, it needs more thought. Some further experimentation shows that this already does work in a block without a session argument. I’m not sure why it appeared that it did not before to me, but it was my mistake.