0
votes

I am currently stuck on the rails tutorial from Michael Hartl (railstutorial.org), Chapter 13

and getting the following two errors:

1) Error:
MicropostsControllerTest#test_should_redirect_create_when_not_logged_in:
ActionController::UrlGenerationError: No route matches {:action=>"/microposts", :controller=>"microposts", :params=>{:micropost=>{:content=>"Lorem ipsum"}}}
    test/controllers/microposts_controller_test.rb:11:in 'block (2 levels) in <class:MicropostsControllerTest>'
    test/controllers/microposts_controller_test.rb:10:in 'block in <class:MicropostsControllerTest>'


  2) Error:
MicropostsControllerTest#test_should_redirect_destroy_when_not_logged_in:
ActionController::UrlGenerationError: No route matches {:action=>"/microposts/499495288", :controller=>"microposts"}
    test/controllers/microposts_controller_test.rb:18:in 'block (2 levels) in <class:MicropostsControllerTest>'
    test/controllers/microposts_controller_test.rb:17:in 'block in <class:MicropostsControllerTest>'

As far as i know, 'action' should be something like get, post, delete etc. But i don't know, why it says 'micropost' here.

Content of microposts_controller_test.rb:

require 'test_helper'

class MicropostsControllerTest < ActionController::TestCase

  def setup
    @micropost = microposts(:orange)
  end

  test 'should redirect create when not logged in' do
    assert_no_difference 'Micropost.count' do
      post microposts_path, params: {micropost: {content: 'Lorem ipsum'}}
    end
    assert_redirected_to login_url
  end

  test 'should redirect destroy when not logged in' do
    assert_no_difference 'Micropost.count' do
      delete micropost_path(@micropost)
    end
    assert_redirected_to login_url
  end
end

Content of micropost_controller.rb:

class MicropostsController < ApplicationController
  before_action :logged_in_user, only: [:create, :destroy]

  def create
    @micropost = current_user.microposts.build(micropost_params)
    if @micropost.save
      flash[:success] = 'Micropost created!'
      redirect_to root_url
    else
      render 'static_pages/home'
    end
  end

  def destroy
  end

  private

  def micropost_params
    params.require(:micropost).permit(:content)
  end

end

Content of routes.rb:

Rails.application.routes.draw do
  root 'static_pages#home'
  get '/help' => 'static_pages#help'
  get '/about' => 'static_pages#about'
  get '/contact' => 'static_pages#contact'
  get '/signup' => 'users#new'
  get '/login' => 'sessions#new'
  post '/login' => 'sessions#create'
  delete '/logout' => 'sessions#destroy'
  resources :users
  resources :account_activations, only: [:edit]
  resources :password_resets, only:     [:new, :create, :edit, :update]
  resources :microposts, only:           [:create, :destroy]
end

Any help is appreciated.

Thanks

Edit:

In micropost_controller_test.rb, it should've been:

class MicropostsControllerTest < ActionDispatch::IntegrationTest

instead of

class MicropostControllerTest < ActionCntroller::TestCase
2
Is this scenario working in UI....Before writing testcases, did you check it on your UI?SnehaT
The problem with the test that you dont have a route in the controller :action=>"/microposts" on which the test is trying to hit and if you see your routes file it confirms resources :microposts, only: [:create, :destroy], so no route for that. Secondly after destroy it should redirect and in your controller method no redirection is there. So eventually test fails.Hassan Abbas

2 Answers

0
votes

The problem with the first test is that you dont have a route in the controller /microposts" on which the test is trying to hit and if you see your routes file it confirms resources :microposts, only: [:create, :destroy], so no route for that. Secondly after destroy it should redirect and in your controller method no redirection is there. So eventually test fails.

0
votes

In micropost_controller_test.rb, it should've been:

class MicropostsControllerTest < ActionDispatch::IntegrationTest

instead of

class MicropostControllerTest < ActionCntroller::TestCase