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 Writing Tests Validating Format

Lisa Rossiter
Lisa Rossiter
3,630 Points

This is wrong

Right in this tutorial he composes a test to validate the the format in his example the profile name clearly has spaces in it so why does it pass!

user.profile_name = "My profile name with spaces"

I cannot get this code to pass.

class UserTest < ActiveSupport::TestCase
  test "A user should enter a first name" do
    #Make a new user
    user = User.new
    #see if it saves the user
    assert !user.save
    #see if it saves with an empty error
    assert !user.errors[:first_name].empty?
  end
  test "A user should enter a last name" do
    user = User.new
    assert !user.save
    assert !user.errors[:last_name].empty?
  end
  test "A user should enter a profile name" do
    user = User.new
    assert !user.save
    assert !user.errors[:profile_name].empty?
  end
  test "A user should have a unique profile name" do
    user = User.new
    user.profile_name = users(:sam).profile_name

    assert !user.save
    assert !user.errors[:profile_name].empty?
  end
  test "A user should have a profile name without space's" do
    user = User.new
    user.profile_name = "My profile name with spaces"

    assert !user.save
    assert !user.errors[:profile_name].empty?
    puts user.errors.inspect
    assert user.errors[:profile_name].include?("Must be formatted correctly")
  end
end

4 Answers

Lisa Rossiter
Lisa Rossiter
3,630 Points

I had to use one of my fixtures in the end. I also tried different reg ex but it ended up being that I needed to provide other user details such as a pass,email... etc

Lisa Rossiter
Lisa Rossiter
3,630 Points
class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :statuses

  #makes sure that these are entered by the user
  validates :first_name, presence: true
  validates :last_name, presence: true
  validates :profile_name, presence: true,
                            uniqueness: true,
                            format: {
                              with: /\A[a-zA-Z\-\_]+\Z/,
                              message: "must be formatted correctly."
                            }

  def full_name
    self.first_name + " " + self.last_name
  end
end

Great tutorials though Jason although it is a bit weird watching you age haha

Lisa Rossiter
Lisa Rossiter
3,630 Points

I am using rails 4 by the way...