2017-07-14 56 views
0

我跟着一个关于使用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 

这里的运行测试时,端子输出:

Terminal output

有什么奇怪的是,当我发表意见线那些正在产生错误的测试,测试在最后一次测试通过时按预期运行。我已经检查了测试文件以及描述相同问题的类似帖子,它在语法上是正确的。 Rails新手如此赦免菜鸟的错误。

回答

1

的问题是,你有一个名为post变量:

post = FactoryGirl.create(:post) # define `post` variable 
post :create, params: ...   # try to call `post` method 

因此,在随后的行post将参考变量,而不是post方法。

解决方案1:重命名变量

my_post = FactoryGirl.create(:post) 
post :create, params: { post_id: my_post.id, ... } 

解决方案2:使用self.post访问irb方法

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 
# ^
0

我认为这与你定义一个名为post然后试图调用Rspec方法post变量做:

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 

尝试重新将其命名为: 调用Rspec方法post

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 
+0

啊这是有道理的。我有一个后期模型,所以我有条件输入所有的测试。谢谢! –