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

Testing the Profiles Controller Challenges 1 and 2

Add an assertion to check that the 'user' controller instance variable exists. Add an assertion to check that the 'statuses' controller instance variable is not empty.

There is a code snippet on this page: http://teamtreehouse.com/library/build-a-simple-ruby-on-rails-application/building-the-profile-page/testing-the-profiles-controller-2

Code Snippets

Test that a controller instance variable exists in a test:

assert assigns(:variable)

However this code snippet didn't answer EITHER (any) of the challenges

By a long process of trial and error (after watching the video many many times I finally came up with):

require 'test_helper'

class ProfilesControllerTest < ActionController::TestCase
  test "that a user instance variable is assigned on a profile view" do
    get :show, id: users(:mikethefrog).profile_name
    assert assigns(:'user')
    assert_not_empty assigns(:'statuses')
  end
end

And there were a whole bunch of forum threads for this code challenge: http://teamtreehouse.com/library/build-a-simple-ruby-on-rails-application/building-the-profile-page/testing-the-profile-name

by none of them had a regex that passed, so I'm totally stuck on that one.

This one seem to be the popular on the forum

(but didn't pass)

validates :profile_name,
            presence: true,
            uniqueness: true,
            format: {
              with: /\A[a-zA-Z0-9_\-]+\z/,
              message: "must be formatted correctly"
            }

also tried countless variations like:

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

3 Answers

your first attempt was almost correct.

ruby symbols look like :symbol_name, not :'symbol_name'

the correct code looks like:

require 'test_helper'

class ProfilesControllerTest < ActionController::TestCase
  test "that a user instance variable is assigned on a profile view" do
    get :show, id: users(:mikethefrog).profile_name
    # remove quotes from symbol names
    assert assigns(:user)
    assert_not_empty assigns(:statuses)
  end
end

Thanks (i already got it to pass with the first set of code I posted above) for that challenge: http://teamtreehouse.com/library/build-a-simple-ruby-on-rails-application/building-the-profile-page/testing-the-profiles-controller

...but what about the other "testing-the-profile-name" regex question? Any clues?

That's the last challenge I need to do and then I'll be done with the Build a Simple Ruby on Rails Application: http://teamtreehouse.com/library/build-a-simple-ruby-on-rails-application/building-the-profile-page/testing-the-profile-name

i dont think you need a regex in the test at all. the validation will be in the model. in the test you just want to make sure the validation is correct. you just have to set the username and assert the user is valid.

jason covers writing the test at 2:41 of this video

the passing code is below

test "a user can be saved with a correctly formatted profile name" do
  user = User.new(first_name: 'Mike', last_name: 'The Frog', email: 'mike@mikethefrog.com')
  user.password = user.password_confirmation = 'lillypads1'

  # Write your code here.
  user.profile_name = "mike_the_frog-123"
  assert user.valid?
end

Thanks Stone, I guess I just totally misunderstood the question.

I search around on other forum posts regarding this challenge and I found:

assert user.valid?

on this forum post: https://teamtreehouse.com/forum/help-ruby-on-rails-testing-the-profile-name

...but guess it didn't pass because it didn't have the other line:

user.profile_name = "mike_the_frog-123"

I'm going to lay off the ruby courses for a while, til the smoke stops coming out my ears... : )

yes you need both lines to pass