Test fixtures provide test data to be used by our automated tests.
rails generate model
command (or via Devise), test fixtures for the model class are also generated.
test/fixtures
Limerick
→ limericks.yml
User
→ users.yml
MultipleChoiceQuestion
would have a fixture file multiple_choice_questions.yml
.limericks.yml
fixtures file in VS Code. You should see entries like this:one:
title: MyString
limerick_text: MyText
two:
title: MyString
limerick_text: MyText
Limerick
objects specified here.
one
and the other is named two
.title
and limerick_text
attributes set to the generated placeholder values MyString
and MyText
, respectively.
users.yml
fixtures file in VS Code. You should see entries like this (comments elided):one: {}
two: {}
User
objects have no attribute values specified, as indicated by the empty braces {}
.
Note that it is a common practice to name each fixture of a given type one
, two
, etc.; however, following this naming convention is not strictly required.
User
fixtures by adding an email
attribute with a realistic value to each user.one:
email: alice@email.com
two:
email: bob@email.com
Note that we deleted the empty braces {}
, which denote that a fixture has no attributes.
Note that for User
fixtures, it is unnecessary to add any other attributes (such as password).
Update the Limerick
fixtures’ title
and limerick_text
attribute values to be more realistic by editing them in VS Code as in the code below.
Additionally, recall that there is a has_many
/belongs_to
association between the User
and Limerick
classes. Because the belongs_to
declaration includes an implicit validation that an associated object is present, we must add to each of our Limerick
fixtures an association link to a User
fixture. To accomplish this, we must add an additional association-link attribute to each Limerick
fixture. To determine the name of the association-link attribute to add, inspect the belongs_to
declaration in the Limerick
class, and note that :author
is the name specified for the associated User
object. Thus, we add an author
attribute to each of our Limerick
fixtures as follows.
one:
title: There once was a farmer from Leeds
limerick_text: "There once was a farmer from Leeds,\nWho swallowed a packet of seeds.\nIt soon came to pass,\nHe was covered with grass,\nBut has all the tomatoes he needs."
author: one
two:
title: A canner, exceedingly canny
limerick_text: "A canner, exceedingly canny\nOne morning remarked to his granny,\n\"A canner can can\nAnything that he can;\nBut a canner can't can a can, can he?\""
author: one
limerick_text
string values contain special escape sequences (\n
to print a newline and \"
to print a quotation mark), those strings must be surrounded in quotation marks ("
).
title
string values do not contain escape sequences and therefore need not be surrounded in quotation marks.author
attribute, and each such attribute is set to one
.
one
in this case refers to the User
fixture named one
from the users.yml
fixture file.Limerick
feature objects belongs to the same User
fixture object (one
).User
fixture object two
has no Limerick
objects in these fixtures.