My posts_controller_Test.rb file:
# test/controllers/posts_controller_test.rb
require 'test_helper'
class PostControllerTest < ActionController::TestCase
def setup
@post = Post.new
end
test 'should get index' do
get :index
assert_response :success
assert_not_nil assigns(:posts)
end
end
My post_controller File
class PostsController < ActionController::Base
before_action :authenticate_user!
def index
@post = current_user.posts.paginate(page: params[:page], per_page: 5)
respond_to do |format|
format.html
format.json { render json: @post }
end
end
def new
@post = Post.new
end
def create
@post = current_user.posts.build(post_param)
if @post.save
redirect_to action: 'index'
else
render 'new'
end
end
Why my test cases
are failing? Is it because I have authenticate_user!
condition? I also have .yml
file and tried to test with it but after initializing with .yml
data I am getting RuntimeError: @controller is nil: make sure you set it in your test's setup method.
yml file
one:
data 2
value: 3
user_id: 1
name: 'test'
.yml has everything what i have required in
params.require(:post).permit(:data, :value, :name) and obvious `user` for foreign key reference
Edit -1
After the suggestion of inheriting with ApplicationController
Got this new error:
NameError: uninitialized constant ApplicationController::Base
Aucun commentaire:
Enregistrer un commentaire