我跟着一个关于使用Rspec进行测试的教程。尝试运行测试时出现语法错误。这里是我的测试代码:在Rspec上加载文件错误
require 'rails_helper'
RSpec.describe CommentsController, type: :controller do
describe "comments#create action" do
it "should allow admins to create comments on posts" do
post = FactoryGirl.create(:post)
admin = FactoryGirl.create(:admin)
sign_in admin
post :create, params: { post_id: post.id, comment: { message: 'awesome post' } }
expect(response).to redirect_to root_path
expect(post.comments.length).to eq 1
expect(post.comments.first.message).to eq "awesome gram"
end
it "should require an admin to be logged in to comment on a post" do
post = FactoryGirl.create(:post)
post :create, params: { post_id: post.id, comment: { message: 'awesome post' } }
expect(response).to redirect_to new_admin_session_path
end
it "should return http status code of not found if the post isn't found" do
admin = FactoryGirl.create(:admin)
sign_in admin
post :create, params: { post_id: 'SUNSHINE', comment: { message: 'awesome post'} }
expect(response).to have_http_status :not_found
end
end
end
这里的控制器:
class CommentsController < ApplicationController
before_action :authenticate_admin!, only: [:create]
def create
@post = Post.find_by_id(params[:post_id])
return render_not_found if @post.blank?
@post.comments.create(comment_params.merge(admin: current_admin))
redirect_to root_path
end
private
def render_not_found(status=:not_found)
render plain: "#{status.to_s.titleize} :(", status: status
end
def comment_params
params.require(:comment).permit(:message)
end
end
这里的运行测试时,端子输出:
有什么奇怪的是,当我发表意见线那些正在产生错误的测试,测试在最后一次测试通过时按预期运行。我已经检查了测试文件以及描述相同问题的类似帖子,它在语法上是正确的。 Rails新手如此赦免菜鸟的错误。
啊这是有道理的。我有一个后期模型,所以我有条件输入所有的测试。谢谢! –