为什么 rails minitest 测试用例失败
Why rails minitest testcases are failing
我的 posts_controller_Test.rb 文件:
# 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
我的post_controller文件
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
为什么我的 test cases
失败了?是因为我有authenticate_user!
条件吗?我也有 .yml
文件并尝试用它进行测试但是在用 .yml
数据初始化后我得到 RuntimeError: @controller is nil: make sure you set it in your test's setup method.
yml 文件
one:
data 2
value: 3
user_id: 1
name: 'test'
.yml 拥有我在
中需要的一切
params.require(:post).permit(:data, :value, :name) and obvious `user` for foreign key reference
编辑-1
建议继承后ApplicationController
收到这个新错误:
NameError: uninitialized constant ApplicationController::Base
app/controllers/posts_controller.rb:3:in `<top (required)>'
app/controllers/posts_controller.rb:3:in `<top (required)>'
这是我的第 3 行:
class PostsController < ApplicationController::Base
编辑-2
class PostsController < ApplicationController
知道了:
NoMethodError: undefined method `authenticate!' for nil:NilClass
错误是因为您从错误的地方继承了您的控制器class。而是从 ApplicationController 继承:
class PostsController < ApplicationController
...
end
我的 posts_controller_Test.rb 文件:
# 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
我的post_controller文件
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
为什么我的 test cases
失败了?是因为我有authenticate_user!
条件吗?我也有 .yml
文件并尝试用它进行测试但是在用 .yml
数据初始化后我得到 RuntimeError: @controller is nil: make sure you set it in your test's setup method.
yml 文件
one:
data 2
value: 3
user_id: 1
name: 'test'
.yml 拥有我在
中需要的一切params.require(:post).permit(:data, :value, :name) and obvious `user` for foreign key reference
编辑-1
建议继承后ApplicationController
收到这个新错误:
NameError: uninitialized constant ApplicationController::Base
app/controllers/posts_controller.rb:3:in `<top (required)>'
app/controllers/posts_controller.rb:3:in `<top (required)>'
这是我的第 3 行:
class PostsController < ApplicationController::Base
编辑-2
class PostsController < ApplicationController
知道了:
NoMethodError: undefined method `authenticate!' for nil:NilClass
错误是因为您从错误的地方继承了您的控制器class。而是从 ApplicationController 继承:
class PostsController < ApplicationController
...
end