I have a web application in ruby on rails, that is mainly a big multi-step form that insert data into a db. I want to make integration test to verify all the inserted data is correctly saved.

My idea is to make a factory to create a dummy record, insert all the data in the form using selenium, then comparing the db to the dummy record. Is this correct?

I don't like the "comparing" and "inserting" part, because it adds complexity INSIDE the test. Should i just hard-code all the data?

  • I don't like the "comparing" and "inserting" part fairly true. Instead of asserting that DB works, put the focus on the business. In this case, a valid assertion could be that after "step 1" with data "x" you get redirected to "step 2" or bak to "step 1" with error messages. Do test against business. – Laiv Nov 27 '18 at 15:18
  • But what if i want to check that the data is actually correctly saved? – Grey Nov 28 '18 at 12:43
  • If data were not saved correctly, you would not be redirected to "step 2", you would be back to step 1 with some error messages in it. You should validate that. Think like there were not any database. You send data to the server and you validate the data when you do the request of it. your test should focus on testing your application, whether the data is stored in DB or in flat text files doesn't matter. At least not in this kind of tests. – Laiv Nov 28 '18 at 14:11
  • The problem still is about the data sets...if i want to test 20 different use cases with 20 different inputs, do I need to set the data manually in every in test, with a lot of replication? Using factory girl would be much more DRY. – Grey Dec 17 '18 at 14:18

This depends on what fidelity is required for the test. Sometimes it may be sufficient to test on a level of simulated HTTP requests:

Given an API
When I POST /the-form with key=value
Then the response is {"some": "data", "key": "value"}

This is suitable as an integration test for an API or backend, but probably not for a HTML form. E.g. fields might be missing.

You get higher fidelity tests if you use some automation framework that parses the HTML, and e.g. allows you to fill out forms by their CSS selector or label and to query data by CSS selectors.

Given I am on the page /the-form
When I fill out key with value
And submit the form
Then .datasheet > .key-value is value
And .datasheet > .title contains “your data has been processed successfully”

For web pages with lots of JavaScript (e.g. SPAs) you need to run a real browser. Selenium helps there. But this level of fidelity might not be required for a typical Rails app. The more realistic your tests are, the slower they will typically be.

It is normal that your test cases contain concrete scenarios with example data that you run through. You might consider this “hard-coding”, but concrete examples are much easier to understand than abstract data set factories. Nevertheless, many testing tools allow you to parameterize your scenario over multiple example data sets.

When running these scenarios, the test should be clear about what the system under test is. For browser-based tests, you're likely performing end to end tests of your complete system, sans any external services. And you limit your tests to observing what a normal user would see. Therefore, you shouldn't verify that some record has been created in the database – the existence and structure of a database is an implementation detail. Instead, you would verify that the web interface shows the correct data after some action has been taken.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.