Bummer! This is just a preview. You need to be signed in with a Treehouse account to view the entire video.
Fixtures2:56 with Jason Seifer
Fixtures will populate a test database with certain information every time a test is run. Fixtures are useful for setting up different states of an application and data when testing a Ruby on Rails web application.
If you get an error like the following while running your tests:
ActiveRecord::StatementInvalid: SQLite3::SQLException: table users has no column named password
Try opening up the test/fixtures/users.yml file and remove the password and password_confirmation lines.
When testing our applications, we want certain data to be available to all of our tests.
Rails makes this easy with something called fixtures.
In this video we're going to learn to work with fixtures while testing our application.
What we're going to do now in order to make this test pass is 2 things.
We're going to create a fixture for our users.
Fixtures contain seed data for our database
and are very useful when setting up different states for our application
in order to test it.
Here's how we create a fixture for a user.
I'm going to create a fixture for a user named Jason.
And I'm going to enter the data that I want when I refer to this user.
This is not my actual password.
Now that I have this data set up, I shouldn't see a failure when we run this code
because I haven't put any validations for the profile name.
However, now I should not see any failures for the profile name when I run this test.
But it looks like I have a different failure,
and the failure now occurred on line 32.
What we're going to do is add another validation here for uniqueness.
What this will do is perform a lookup in our database to see if another user exists
with the same profile name when we try and save this user.
And it looks like we get the correct passing message.
If we wanted to refer to the user in our fixtures,
we would do that by typing users and then a colon
and the name that we put up here for the name of the fixture.
This is how it would look.
In fact, we could do the same thing by just assigning it to the existing variable.
And now our test still passes.
You need to sign up for Treehouse in order to download course files.Sign up