Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Fixtures, uniqueness validation.

I'm watching the "Uniqueness Validation" video of the Building a Simple Rails Application series. I'm slightly confused as to how the uniqueness validation test actually proves anything.

The test only puts one name into the test database, and the validation validates that it is indeed unique. But it only puts one name in, so of course it's going to be unique?

Surely a valid test would be to put two entries into the test database with the same profile name, and then assert that user errors exist by running:

    assert !user.errors[:profile_name].empty?

at the end of the test?

I tried doing this test myself, but I can't figure out how to put two "users" into the users.yml fixture. But this,

jason:
 first_name: "Jason"
 last_name: "Seifer"
 profile_name: "Hiya"
 email: "jason@email.com"

jim:
 first_name: "Jim"
 last_name: "Jimson"
 profile_name: "Hiya"
 email: "jim@email.com"

results in a

ActiveRecord::Fixture::FormatError: a YAML error occurred parsing

error.

1 Answer

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Hi Jonathan hurley, it would be a little better to put different profile names in the users fixture and then then create one you know isn't unique in a test for it later or you may run in to some weird behavior in other tests. The error you're seeing is probably due to indentation -- YAML is indented using two spaces and it looks like there's only one in the examples here.