Fixtures are very much relevant in Rails testing. Fixtures are a way of organizing your test data.Testing the application helps to debug it more efficiently and ensures the desired functionality to the application. Let’s have a look on them.
It is used to manage the test data. It tests against the real data and is written in YAML files. For each model in the application, there is a .yml file in the test/fixtures
directory. When we generate the model using rails g it will automatically create the .yml file also. Here you can see an example,
Matz: name: redpanthers message: hiHere ‘Matz’ is the fixture name. And the name and message are the fields in the User model. Fixtures come in 3 flavours:
- YAML fixtures: It is a file format which describes data structures in human-readable format.These are stored in a single file per model(above example is in yaml format).
- CSV fixtures: Here values are kept in the Comma Separated Value (CSV) format. These are stored in a single file but instead end with the .csv file extension.
Eg:
name, message redpanthers, hi
- Single-file fixtures: These are the original format for Active Record.
Eg:
name => redpanthers message => hi
Creating multiple/random items in fixtures
You can create multiple items as:Adorn: name: redpanthers message: hi Alex: name: google message: hello .............When you add fixtures, they get IDs based on a hash of the table name and the fixture name. To us humans, they just look like random numbers. So there is no need to define the id for this. It will ensure that the id is unique for every item. Fixtures are unordered. If you want ordered items, use the omap YAML type. Also, you can access the data in the test as:
test "find" do assert_equal "User", users(:Adorn).name endWhere ‘users’ is the name of the yml file and ‘Adorn’ is the fixture name.
ERB Fixtures
You can add ERB with your YAML fixtures to create a bunch of fixtures as:<% 1000.times do |i| %> user_<%= i %>: name: <%= "user#{i}" %> message: <%= "message#{i}" %> <% end %>In the above example, the code generate 1000 users.
Writing belongs_to/has_many relationships
We can also define associations between fixtures. For example, if a user has so many customers there exists a has_many/belongs to a relationship. So we can get the value as:# In fixtures/customers.yml Jacob: name: Tiago # In fixtures/users.yml David: name: redpanthers message: hi customer: JacobLike above, you can get customer details in users.yml.
Why are they Special?
- It allows you to populate your test database with values so that you can test more conveniently without having to put lots of data into it when setting up a test run.
- These are the instances of Active Record. So you can access the object directly because it is automatically setup as a local variable of the test case.
Eg:
# this will return the User object for the fixture named David Adam(:David)
- We can set a value to nil/blank from the fixture.
Eg:
require 'yaml' YAML.load "--- \n:private: null\n") => {:private=>nil} # In fixture Abner: name: john private: nullI hope this article helps you in a better way to know the basics about fixtures!