I’m working on an EmberJS frontend app and want to run integration tests against a real Rails backend. The main issue is keeping the database state consistent between tests.
I’m thinking about making a special API in Rails that the frontend can use to set up the right database state before each test. This way, we can:
- Test against the actual backend
- Check for API inconsistencies
- Verify backend business logic
- Avoid duplicating backend behavior in mocks
Here’s what I’m picturing:
- Rails runs normally but with a special flag
- Frontend calls the API before each test
- Rails clears the database and sets up the requested data
- Frontend runs the test using the normal API
- Process repeats for the next test
I’m not super familiar with Rails. I’ve heard of Factory Girl and Database Cleaner but don’t know how to use them outside of Rails’ test environment.
Any ideas on how to make this work? It would be great if the frontend could define the fixture properties too.
If this works out, I’d love to turn it into a gem for others to use. Thanks for any help!
I’ve tackled a similar challenge in my projects and can share some insights.
Instead of creating a special API, consider using a shared fixture library between the Rails backend and the EmberJS frontend. This approach allows you to define data structures once and reuse them in both environments.
On the Rails side, leveraging tools like FactoryBot (formerly FactoryGirl) and DatabaseCleaner can help manage test data effectively. On the EmberJS side, using a library such as Mirage JS to mock your API allows you to mirror backend fixtures while keeping tests isolated.
For integration tests, consider setting up a separate Rails environment with an endpoint to reset the database and load specific fixtures. This keeps a clear separation of concerns while ensuring your tests run with consistent data.
hey, i’ve dealt with this before. instead of a special API, try using shared fixtures between rails and emberjs. it’s way easier.
use factorybot and databasecleaner in rails. for emberjs, mirage js is awesome for mocking apis.
for integration tests, set up a separate rails env with an endpoint to reset the db and load fixtures. keeps things clean and consistent.
interesting approach! have you thought about using a shared test database? how would you tackle race conditions with simultaneous tests? and have you tried cypress for end-to-end testing? whats your biggest concern with the current setup?