Mia Genovese
Mia Genovese

Reputation: 45

ruby on rails tutorial chapter 8 test error

I'm getting two different test errors in Chapter 8.2.1 of Michael Hartl's Ruby on Rails Tutorial. I'm new to Rails, but I have triple checked for syntax errors and anything else I could find. Any help is very much appreciated.

Error Message 1: ERROR["test_should_get_new", Minitest::Result, 0.9693643249993329] test_should_get_new#Minitest::Result (0.97s) NameError: NameError: undefined local variable or method users_new_url' for #<UsersControllerTest:0x00000006e953f8> test/controllers/users_controller_test.rb:5:inblock in '

Error Message 2: ERROR["test_invalid_signup_information", Minitest::Result, 0.8977870759990765] test_invalid_signup_information#Minitest::Result (0.90s) ActionController::RoutingError: ActionController::RoutingError: No route matches [POST] "/signup" test/integration/users_signup_test.rb:8:in block (2 levels) in <class:UsersSignupTest>' test/integration/users_signup_test.rb:7:inblock in '

Routes.rb

    Rails.application.routes.draw do

  root   'static_pages#home'
  get    '/help',    to: 'static_pages#help'
  get    '/about',   to: 'static_pages#about'
  get    '/contact', to: 'static_pages#contact'
  get    '/signup',  to: 'users#new'
  get    '/login',   to: 'sessions#new'
  post   '/login',   to: 'sessions#create'
  delete '/logout',  to: 'sessions#destroy'
  resources :users

end

Sessions Controller:

    class SessionsController < ApplicationController

  def new
  end

  def create
    user = User.find_by(email: params[:session][:email].downcase)
    if user && user.authenticate(params[:session][:password])
      # Log the user in and redirect to the user's show page.
    else
      flash.now[:danger] = 'Invalid email/password combination'
      render 'new'
    end
  end

  def destroy
  end
end

Users Controller:

    class UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
  end

  def new
    @user = User.new
  end
  def create
    @user = User.new(user_params)
    if @user.save
      flash[:success] = "Welcome to your new profile!"
      redirect_to @user
    else
      render 'new'
    end
  end

  private

    def user_params
      params.require(:user).permit(:name, :email, :password,
                                   :password_confirmation)
    end

end

Users controller test

    require 'test_helper'

class UsersControllerTest < ActionDispatch::IntegrationTest
  test "should get new" do
    get users_new_url
    assert_response :success
  end

end

Users Signup Test

    require 'test_helper'

class UsersSignupTest < ActionDispatch::IntegrationTest

  test "invalid signup information" do
    get signup_path
    assert_no_difference 'User.count' do
      post users_path, params: { user: { name:  "",
                                         email: "user@invalid",
                                         password:              "foo",
                                         password_confirmation: "bar" } }
    end
    assert_template 'users/new'

  assert_select 'div#error_explanation'
  assert_select 'div.field_with_errors'
  assert_select 'form[action="/signup"]'
  end

  test "valid signup information" do
    get signup_path
    assert_difference 'User.count', 1 do
      post users_path, params: { user: { name: "Example User",
                                        email: "[email protected]", 
                                        password:             "password",
                                        password_confirmation: "password" } }
    end
    follow_redirect!
    assert_template 'users/show'
    assert_not flash.nil?
  end

end

Upvotes: 0

Views: 646

Answers (1)

James Milani
James Milani

Reputation: 1943

In your users_controller_test, the code doesn't know what users_new_url is. This is probably because that route doesn't exist. You will most likely have to do something like get new_user_path, but you can find out by typing rake routes and getting the list of your available routes.

Here's an example of what rake routes will output:

users     GET     /users(.:format)                    users#index
          POST    /users(.:format)                    users#create
new_user  GET     /users/new(.:format)                users#new
edit_user GET     /users/:id/edit(.:format)           users#edit
user      GET     /users/:id(.:format)                users#show

You can reference a named path by appending _path to the name. i.e. users_path will map to the "users#index"controller and method.

rake routes will also help for your second problem, which is that you do not have a route defined for POST /signup -- you have a GET /signup.

So, you'll need to add a line like:

post '/signup', to: 'users#create'

This route will map to your UsersController's #create method, which I see you have in your code.

Upvotes: 1

Related Questions