无法弄清楚这个错误有什么问题:语法错误,意外的tLABEL,期待'=',在我的Rspec测试中?

时间:2016-09-07 15:41:13

标签: ruby-on-rails rspec

我收到此错误:

comments_controller_spec.rb:12: syntax error, unexpected tLABEL, expecting '='
...ate, post_id: post.id, comment: attributes_for(:comment)}.to...
                           ^

运行我的spec文件comments_controller_spec.rb:

require 'rails_helper'
describe CommentsController do 

  describe "POST #create" do 
    comment = create(:comment)
    post = comment.post

    it "properly creates a comment" do 
      expect{post :create, post_id: post.id, comment: attributes_for(:comment)}.to change(Comment, :count).by(1)
    end

  end
end

从我从阅读其他帖子中收集到的内容是我没有正确关闭块或哈希。我一直在看这个,我找不到任何未正确关闭的东西。它似乎指向我的post方法作为问题的根源,但我相信我已经正确地传递了它的所有参数。所以我不确定还有什么可能是错的。谁能告诉我们这里发生了什么?谢谢你的帮助。

更新:

我把测试改为:

it "properly creates a comment" do 

      expect {post(:create, post_id: post.id, comment: attributes_for(:comment))}.to change(Comment, :count).by(1)
end

现在错误信息是:

CommentsController POST #create properly creates a comment
     Failure/Error: expect {post(:create, post_id: post.id, comment: attributes_for(:comment))}.to change(Comment, :count).by(1)

     ArgumentError:
       wrong number of arguments (given 2, expected 0)
     # ./spec/controllers/comments_controller_spec.rb:13:in `block (4 levels) in <top (required)>'
     # ./spec/controllers/comments_controller_spec.rb:13:in `block (3 levels) in <top (required)>'

这是我的post_factory.rb:

FactoryGirl.define do 

  factory :post do 
    content "this is post content!"
    user
  end
end

2 个答案:

答案 0 :(得分:0)

我相信change匹配器需要一个块:

expect{post :create, post_id: post.id, comment: attributes_for(:comment)}.to change{ Comment.count }.by(1)

如果不是这样,我会尝试在post的参数周围添加括号,以为解析器可能存在问题:

expect{ post(:create, post_id: post.id, comment: attributes_for(:comment)) }.to change{ Comment.count }.by(1)

作为最后的手段,我会这样做:

expect do
  post(:create, post_id: post.id, comment: attributes_for(:comment)) 
end.to change{ Comment.count }.by(1)

答案 1 :(得分:0)

尝试以下方法:

var myBinding = new BasicHttpBinding();
myBinding.Security.Mode = SecurityMode.TransportCredentialOnly;