Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

General Discussion

Brad Halstead
Brad Halstead
5,662 Points

Test factories user_friendship_controller_test failures

Hi, I've ran a rake test and got a bunch of failures. I spent a few hours trying to find what I expect to be a typo but I couldn't find anything.

here is the result of running rake test

Brad@BRAD-HP /c/projects/spinminute-ruby/spinminute $ rake test Run options:

Running tests:

.........................

Finished tests in 1.833105s, 13.6381 tests/s, 18.5478 assertions/s.

25 tests, 34 assertions, 0 failures, 0 errors, 0 skips Run options:

Running tests:

.................FFFFF.............

Finished tests in 3.549203s, 9.8614 tests/s, 15.2147 assertions/s.

1) Failure: test: #create when logged in with a valid friend_id should assign a friend objec t. (UserFriendshipsControllerTest) [c:/projects/spinminute-ruby/spinminute/test/ functional/user_friendships_controller_test.rb:104]: Failed assertion, no message given.

2) Failure: test: #create when logged in with a valid friend_id should assign a user_friends hip object. (UserFriendshipsControllerTest) [c:/projects/spinminute-ruby/spinmin ute/test/functional/user_friendships_controller_test.rb:109]: Failed assertion, no message given.

3) Failure: test: #create when logged in with a valid friend_id should create a friendship. (UserFriendshipsControllerTest) [c:/projects/spinminute-ruby/spinminute/test/fun ctional/user_friendships_controller_test.rb:115]: Failed assertion, no message given.

4) Failure: test: #create when logged in with a valid friend_id should redirect to the profi le page of the friend. (UserFriendshipsControllerTest) [c:/projects/spinminute-r uby/spinminute/test/functional/user_friendships_controller_test.rb:120]: Expected response to be a redirect to http://test.host/pamsue05 but was a redi rect to http://test.host/

5) Failure: test: #create when logged in with a valid friend_id should set the flash success message. (UserFriendshipsControllerTest) [c:/projects/spinminute-ruby/spinminut e/test/functional/user_friendships_controller_test.rb:124]: Failed assertion, no message given.

35 tests, 54 assertions, 5 failures, 0 errors, 0 skips Run options:

Running tests:

F....

Finished tests in 1.398080s, 3.5763 tests/s, 4.2916 assertions/s.

1) Failure: test_that_adding_a_friend_works(AddAFriendTest) [c:/projects/spinminute-ruby/spi nminute/test/integration/add_a_friend_test.rb:13]: Expected response to be a <:success>, but was <302>

5 tests, 6 assertions, 1 failures, 0 errors, 0 skips Errors running test:functionals! #<RuntimeError: Command failed with status (5): [ruby -I"lib;test" -I"c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rake -10.0.4/lib" "c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rake-10.0.4/l ib/rake/rake_test_loader.rb" "test/functional/**/*_test.rb" ]> Errors running test:integration! #<RuntimeError: Command failed with status (1): [ruby -I"lib;test" -I"c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rake -10.0.4/lib" "c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rake-10.0.4/l ib/rake/rake_test_loader.rb" "test/integration/**/*_test.rb" ]>

1 Answer

Brad Halstead
Brad Halstead
5,662 Points

here is my user_friendship_controller_test

require 'test_helper'

class UserFriendshipsControllerTest < ActionController::TestCase

context "#index" do
    context "when not logged in" do
        should "redirect to the login page" do
            get :index 
            assert_response :redirect
        end
    end
end

context "#new" do
    context "when not logged in" do
        should "redirect to the login page" do
            get :new 
            assert_response :redirect
        end
    end

    context "when logged in" do
        setup do
            sign_in users(:brad)
        end

        should "get new and return success" do
            get :new
            assert_response :success
        end

        should "should set a flash error if the friend_id params is missing" do
            get :new, {}
            assert_equal "Friend required", flash[:error]
        end

        should"display the friends name" do
            get :new, friend_id: users(:pam)
            assert_match /#{users(:pam).full_name}/, response.body
        end

        should "assign a new user friendship" do
            get :new, friend_id: users(:pam)
            assert assigns(:user_friendship)
        end

        should "assign a new user friendship to the correct friend" do
            get :new, friend_id: users(:pam)
            assert_equal users(:pam), assigns(:user_friendship).friend
        end

        should "assign a new user friendship to the currently logged in user" do
            get :new, friend_id: users(:pam)
            assert_equal users(:brad), assigns(:user_friendship).user
        end

        should "return a 404 status if no friend is found" do
            get :new, friend_id: 'invalid'
            assert_response :not_found
        end

        should "ask if you really want to request this friendship" do
            get :new, friend_id: users(:pam)
            assert_match /Do you really want to friend #{users(:pam).full_name}?/, response.body
        end

    end
end     

context "#create" do
    context "when not logged in" do
        should "redirect to the login page" do
            get :new
            assert_response :redirect
            assert_redirected_to login_path
        end
    end

    context "when logged in" do
        setup do
            sign_in users(:brad)
        end

        context "with no friend_id" do
            setup do
                post :create
            end

            should "set the flash error message" do
                assert !flash[:error].empty?
            end

            should "redirect to the site root" do
                assert_redirected_to root_path
            end
        end

        context "with a valid friend_id" do
            setup do
                post :create, friend_id: users(:pam)
            end

            should "assign a friend object" do
                assert assigns(:friend)
                assert_equal users(:pam), assigns(:friend)
            end

            should "assign a user_friendship object" do
                assert assigns(:user_friendship)
                assert_equal users(:brad), assigns(:user_friendship).user
                assert_equal users(:pam), assigns(:user_friendship).friend
            end

            should "create a friendship" do
                assert users(:brad).pending_friends.include?(users(:pam))
            end

            should "redirect to the profile page of the friend" do
                assert_response :redirect
                assert_redirected_to profile_path(users(:pam))
            end

            should "set the flash success message" do
                assert flash[:success]
                assert_equal "You are now friends with #{users(:pam).full_name}", flash[:success]
            end
        end
    end
end

end