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 Build a Simple Ruby on Rails Application Testing the Whole App Testing Statuses

Mike Mayer
Mike Mayer
2,151 Points

Why is the line "status.content = "H"' used in the test for verify a content length of 2 characters?

The video describes writing a test to make sure the content value for a status has a minimum of 2 characters, yet the test code seems to use a status that only has one character, the capital H. Why does this pass?

3 Answers

Brandon Barrette
Brandon Barrette
20,485 Points

So what's happening in this test is it says:

  • Give me a status with only 1 character
  • Assert that it doesn't save
  • Assert that it throws an error

It initially doesn't pass because without the validation length: { minimum: 2 }, a status with any length works. So once we add the validation, it won't save and it will throw an error, exactly what our test was asking for.

Hope that helps! Happy coding! =)

Juan Ordaz
Juan Ordaz
12,012 Points

UPDATED CODE RAILS 4

require 'test_helper'

class StatusTest < ActiveSupport::TestCase
  test "that a status requires content" do
    status = User.new
    #make sure is not save
    status.content = nil?
    assert !status.save, "Content must be more than 2 characters"
    #and give me an error message
    assert !status.errors[:content].empty?
  end

  test "that a status's content is at least 2 letters" do
    status = Status.new
    status.content = "h"
    assert !status.save, "Content must be more than 2 characters"
    assert !status.errors[:content].empty?
  end

  test "that a status has a user id" do
    status = Status.new
    status.content = "Hello"

    assert !status.save
    assert !status.errors[:user_id].empty?
  end

end

In your Status Model

class Status < ActiveRecord::Base

    # Association
    belongs_to :user

    #Validation
    validates :content, presence: true,
                                length: { minimum: 2,
                                                message: "Content must be more than 2 characters"
                                            }

    validates :user_id, presence: true
end