Ruby on rails Rspec上的加载文件错误
下面是关于使用Rspec进行测试的教程。尝试运行测试时出现语法错误。以下是我的测试代码:Ruby on rails Rspec上的加载文件错误,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,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 = FactoryGir
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新手,请原谅新手的错误 问题在于您有一个名为post的变量: 因此,在后续行中,post将引用变量,而不是post方法 解决方案1:重命名变量 解决方案2:使用self.post访问该方法 在irb中复制问题:
我认为这与您定义一个名为post的变量,然后尝试调用Rspec方法post有关: 尝试重新命名: 要调用Rspec方法post:
啊这是有道理的。我有一个post模型,所以我习惯于在所有测试中输入它。谢谢
post = FactoryGirl.create(:post) # define `post` variable
post :create, params: ... # try to call `post` method
my_post = FactoryGirl.create(:post)
post :create, params: { post_id: my_post.id, ... }
post = FactoryGirl.create(:post)
self.post :create, params: { post_id: post.id, ... }
def foo
"bar"
end
foo = "wat"
#=> "wat"
foo :hello
# SyntaxError: (irb):63: syntax error, unexpected ':', expecting end-of-input
# foo :hello
# ^
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 require an admin to be logged in to comment on a post" do
message = FactoryGirl.create(:post)
post :create, params: { post_id: message.id, comment: { message: 'awesome post' } }
expect(response).to redirect_to new_admin_session_path
end