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
Felicia Wijaya
Courses Plus Student 14,748 PointsUsing fixtures with association
I have been following along on the Ruby track and I'm trying to write a test where the status should be no more than 150 characters. The status table contain user_id that is linked to the user in user table. Is this the right way to do it?
The test past but is it passing for the right reasons?
in users.yml
cassandra: first_name: "Cassandra" last_name: "Contiago" email: "cassandra@yahoo.com" profile_name: "Cassie"
in statuses.yml
one: user: cassandra content: A super long status that is over 150 characters and should not be saved. I am learning how to code at the moment and it's incredibly hot to stay in front of the laptop but I will persevere.
in status.rb ... validates :content, length: {maximum: 150}, allow_blank: true ...
in status_test.rb require 'test_helper'
class StatusTest < ActiveSupport::TestCase test "a status should be no more than 150 characters" do status = Status.new status.content = statuses(:one).content
assert !status.save
assert !status.errors[:content].empty?
end end
1 Answer
Chris Dziewa
17,781 PointsYou should always check in the browser to make sure that the test actually achieved what you wanted it to. Also instead of typing out a random message like you did you could multiply a short string to achieve the length you want and could go into test/unit/status_test.rb and forget about fixtures for that one since it is a model test. Add the following:
test "that a status's content is no more than 150 characters long" do
status = Status.new
status.content = "H" * 151
assert !status.save
assert !status.errors[:content].empty?
end
Here when you multiply the string it repeats and joins the string by the amount of times it has been multiplied.