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
Steve Monsen
5,207 PointsProblem testing Treehouse app w/ fixture
I'm on the Fixtures video of the Treehouse app.
The test "a user should have a unique profile name" passes when I use a new user created in the test code block itself like this:
test "a user should have a unique profile name" do
user = User.new
user.email = "kurtz8763@gmail.com"
user.first_name = "Steve"
user.last_name = "Monsen"
user.profile_name = "stevem01"
# user.password = "password"
# user.password_confirmation = "password"
# user = users(:steve)
puts "\n====================================>\n" + user.inspect
show_error_messages(user)
assert !user.save
assert !user.errors[:profile_name].empty?
end
But when I try to use a fixture like this it actually saves the new user without any error messages, thereby invalidating the user.save assertion.
test "a user should have a unique profile name" do
user = users(:steve)
puts "\n====================================>\n" + user.inspect
show_error_messages(user)
assert !user.save
assert !user.errors[:profile_name].empty?
end
and the fixtures file:
#test/fixtures/users.yml
steve:
first_name: "Steve"
last_name: "Monsen"
email: "kurtz8763@gmail.com"
profile_name: "stevem01"
# password: "password"
# password_confirmation: "password"
Any ideas?
1 Answer
Jason Seifer
Treehouse Guest TeacherHey Steve Monsen I'm not sure I totally understand the question so please continue to ask questions if I've missed it. In the second example, you are using the fixture that's in the database so it presumably doesn't encounter another profile name that's been taken. In the first example, the fixture with the profile_name does exist to it's running in to that and not saving.
Steve Monsen
5,207 PointsSteve Monsen
5,207 PointsThanks for the reply, but I got it working sometime last night. I'm not sure what I had wrong, but it started working as expected at some point.
Steve Monsen
5,207 PointsSteve Monsen
5,207 PointsOh, ok. I get it now. The reason I wasn't understanding is because I didn't realize what fixtures were actually doing: i.e loading that data into the test db before the test is run. I was just treating fixtures as a shortcut/replacement for creating a test user in the test block itself (w/ user = User.new, etc).
I was all, "Hey, I can just write..."
user = users(:steve)"...and use that for the test user!" Errrrr. Wrong! :)
After re-reading your reply and reading the rails guide on fixtures I get it now.
I figured it was something simple that I wasn't getting like that... a basic paradigm upon which fixtures operate. Thanks, Jason. [slowly swivels head to the side and grins]
Ian Warner
5,563 PointsIan Warner
5,563 PointsI've run into similar errors on this testing part? What line of code ending up making you pass with no errors? I've tried watching the video over and over and trying different things but I can't figure it out. Any help would be much appreciated.
Thanks, Ian