George Bailey wrote:
We do that one in tests to find the last-added record.
Maybe I'm paranoid,
Maybe you are test-infected? (-;
but doesn't that run the risk of a different row
getting created before you can do that?
Yes. I just thought of a fix:
record = assert_latest Record do
get :create_record, :value => 'foo'
assert_equal 'foo', record.value
If we assume monotonic IDs, we can implement that by fetching Record.maximum(id) before the block, then fetching Record.find_by_id(id + 1) after the block. Now we know we have the first new Record which our tested code created. That's probably more robust, and test-isolating, than fetching the very last record!
I think I'l go write that one and retrofit it into a few places...