title: Running integration/acceptance tests on the frontend. Need an API for the frontend to tell Rails which database state to set up for each test
body: <p>My frontend is an EmberJS-based app. It's totally async in nature, so testing it with Capybara is <a href="http://discuss.emberjs.com/t/automated-ember-tests-in-rails-environment/3380/5?u=lolmaus">pain</a> and <a href="https://medium.com/@thatandyrose/testing-ember-js-with-capybara-and-rspec-f793d7ddb49">misery</a>. On the other hand, Ember provides a fantastic test suite out of the box which makes acceptance testing fun and effective.</p>
  
  <p>Normally, either fixtures or backend mocks are used to acceptance-test Ember apps. But testing against mocks does not satisfy me at all:</p>
  
  <ul>
  <li>It will not reveal possible API inconsistencies between the backend and the frontend, race conditions, etc.</li>
  <li>It is impossible to test backend business logic this way. Such tests are <em>not</em> integration tests.</li>
  <li>Finally, acceptance tests require persistency, so you have to replicate backend behavior in a mock. It's very tedious and you effectively end up implementing two backends.</li>
  </ul>
  
  <p>So I want to test against the real backend! It's trivial to set up Ember to use a local backend instance for testing. But <strong>the problem is that the backend will persist its state between individual tests and even test sessions</strong>.</p>
  
  <p>That's why I'm thinking of implementing a special public API in Rails:</p>
  
  <ul>
  <li>The API is only available when Rails is run with a specific flag or an env var.</li>
  <li>Rails runs in a non-testing mode, serving normal API calls as it would in production.</li>
  <li>Before each test, the frontend calls the special API, telling Rails which database setup is needed for this specific test.</li>
  <li>When a call to the special API is received, Rails cleans the database and populates it with the requested data. For example, to test item deletion from the cart, the database should have three items in the cart.</li>
  <li>Rails finishes the API request, and the frontend starts the test.</li>
  <li>Frontend runs test steps, using the normal backend API as it would in production: log in, create posts, comment on them. It will also try doing some forbidden things, e. g. edit posts while not logged in, exceed text length constraints, etc and verify whether the backend rejects forbidden actions.</li>
  <li>When the frontend runs next test, it will call the special API again. Rails will discard the state produced by the previous test and set up a new one, for this specific test.</li>
  </ul>
  
  <p>I'm a frontend dev with a sketchy knowledge of Rails. <a href="https://github.com/thoughtbot/factory_girl">Factory Girl</a> and <a href="https://github.com/DatabaseCleaner/database_cleaner">Database Cleaner</a> seem to be the right tools for the job, but there is absolutely no information how to use them outside Rails' normal test environment. I guess I need a controller or a Rails engine or something.</p>
  
  <p>So the question is: <strong>how do I make an API in Rails that can be used by the frontend to tell Rails to set up a certain database state with a fixture factory</strong>, while Rails are running in a non-test mode i. e. serving REST API and not running through RSpec/Capybara/Cucumber?</p>
  
  <p><strong>Bonus feature</strong>: fixture factory properties should be defined on the frontend so that test code is stored in one place. Thus, the backend should be able to accept fixture factory properties via the special API. Defaults can still be defined in the backend's codebase.</p>
  
  <p>I believe this could become an acceptance/integration testing best practice. If I manage to implement it, I promise to publish a gem.</p>