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: request-spec-example.rb · GitHub
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!