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

Writing Tests > Validating Format

I'm having some trouble with the Validating Format section of the Writing Tests chapter.

First, I'm not getting the same error messages as the one in the video when I omit the validation for proper formatting of profile_name.

My error:

  1) Failure:
test_a_user_should_have_a_profile_name_without_spaces(UserTest) [test/unit/user_test.rb:35]:
<false> is not true.

Jason's error:

  1) Failure:
test_a_user_should_have_a_profile_name_without_spaces(UserTest) [test/unit/user_test.rb:36]:
Failed assertion, no message given.

When I add the format validation, Jason's test passes, but mine still fails.

from user_test.rb:

test "a user should have a profile name without spaces" do
  user = User.new
  user.profile_name = "My Profile With Spaces"

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

from user.rb:

validates :first_name, :presence => true
validates :last_name, :presence => true
validates :profile_name, :presence => true,
                         :uniqueness => true,
                         :format => {
                          :with => /a-zA-Z0-9_-/,
                          :message => 'Must be formatted correctly'
                         }

Note, I had to write with a different syntax because I have a later version of rails installed.

I tested out my validation by trying to register with a profile_name with spaces in it and it was failing, so I think my code in user.rb is correct.

did you ever fix your problem. I'm also having the same problem. If you can help I would appreciate it thanks in advance

19 Answers

Sorry for reviving a bit of a dead thread, but i figured i could help Nick out maybe.

Make sure your message in user.rb and user_test.rb are EXACTLY the same, punctuation and all included. Its what solved it for me.

  • John

Thank You, you solved my problem, wish video explained has to be the same

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Hi Cassidy,

Try changing this:

                      :with => /a-zA-Z0-9_-/,

To this:

                      :with => /\A[a-zA-Z0-9_-]+\Z/,

Jason, just gave that a shot, but I'm still getting the same error.

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Very weird, Cassidy. Can you zip up your project and email it to help@teamtreehouse.com and I'll take a look?

Cassidy, how did you post screenshots of your code like that?

samiff
samiff
31,206 Points

@Robert These forums use Markdown for syntax highlighting. Look at this thread to learn how to format your code to display properly in these forums. Hope that helps!

The forums uses a thing called Markdown which lets you format your text. So to make your code snippets display the way I did it in my original post, your just indent each line. What I did was paste my comment into my text editor and tabbed my code snippets since you can't really do it that easily in these text areas on Treehouse.

There's a bunch of syntax documentation here.

Also, the answer to my original issue was the message in my user.rb file needed a period at the end of it.

I was wondering if anyone figured out the answer to Cassidy's question because I am having the same issue. I would post the images but they won't show for some reason and it's exactly like Cassidy's code.

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Hey Nick, can you email your treebook app to help@teamtreehouse.com and I'll take a look?

I'm getting the exact same issue. I've checked the formatting on the both the test and the user model and it's identical (I copied and pasted).

Here's the project if anyone has a second to check:

https://dl.dropboxusercontent.com/u/33057988/treebook.zip

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Hey Tom Ellis , remove the exclamation point on line 38 -- that is what's causing the test to fail (we are looking to make sure the error is present).

Brian Han
Brian Han
Courses Plus Student 3,959 Points

Thank you, Jason! I was having a similar issue with my own code and this thread as well as many other threads in the forums helped me fix a ton of bugs related to writing tests and registering new users to Treebook.

Anyway, thanks for the support and activity on the forums! Huge help!

Cheers

Awesome, thanks.

Had a similar issue, resolved it by using a different email address and profile_name.

Randy Perez
Randy Perez
5,668 Points

hey guys, still no answer to this question?

having same problem can someone help? Thanks in advance here is my code and still getting failure code

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

# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me,
                :first_name, :last_name, :profile_name
# attr_accessible :title, :body

validates :first_name, presence: true

validates :last_name, presence: true

validates :profile_name, presence: true,
                         uniqueness: true,
                         format: {
                            with: /^[a-zA-Z0-9_-]+$/,
                            message: "Must be formatted correctly."
                         }


has_many :statuses

def full_name
  first_name + " " + last_name
  end
end

my failure code

test_a_user_should_have_a_correctly_formatted_profile_name(UserTest) [test/unit/user_test.rb:44]: Failed assertion, no message given.

this is line 44

assert user.valid?
Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Hey Robert Borsey I think we need to see more of your code to figure out what's up. Can you zip it up and email it to help@teamtreehouse.com?

yes i emailed it 11/15/13 it should be there if not let me know so i can email again thanks for the support

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Hey Robert Borsey I responded to that with two files fixed. If you still have problems drop me a line.

Thanks Jason that fixed the problem :)

Thank you again for the support.