'Rails and RSpec: be_successful vs have_http_status
From what I can see be_successful is core Rails and have_http_status is part of RSpec.
Is there any performance difference (or what is the preferred convention) between:
expect(response).to have_http_status(:success)
expect(response).to be_successful
Thanks
Solution 1:[1]
To your question of performance, there is no difference. Any slight variation in string comparisons or stack depth between the two will be trivial compared to the work of creating and processing the hit to your controller. If you're battling slow tests, focus on avoiding database writes (e.g. Model.build instead of Model.create) and mocking out methods to classes outside the class you're testing.
Aesthetically, I think expect(response).to be_successful is more to the point. Future updates (or even the current implementation) to be_successful may add more insightful tests or feedback than just http_status == 200.
I'd only use the status value check if you were specifically interested in that number for some reason, versus the concept of a successful hit.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | David Hempy |
