← Back to Upcase

How does the thoughtbot team handle pre-building data // test speed in request specs?

In our current request specs - we test both against a json-schema to validate the response structure, but also test some of the response attributes directly to ensure that the correct data-set is being returned from the server.

In doing this, we’ve been using before(:context) to only hit the database once before multiple it and context blocks are fired, as well as using them to only hit the endpoint once per context before a group of it blocks get fired (ex: once without query params and/or headers, another with, etc).

Here is a simple example: https://gist.github.com/thewatts/41ec273cc5d8e02c9d022c2d149c3821

Ultimately, this is all just to make the spec faster (less endpoint hits, and less hits to the database).

Is there a specific way that the thoughtbot team handles this? I know in the testing best practices section of the guides it’s listed to avoid both let and instance_variable usage… so I’m super curious to hear what the thoughtbot team does in this type of situation.

Many thanks!