【问题标题】:Unexpected keyword error when running rspec test [closed]运行 rspec 测试时出现意外的关键字错误 [关闭]
【发布时间】:2015-07-04 23:55:27
【问题描述】:

全部,

运行我的 rspec 测试时,脚本在运行测试之前失败。看起来它在函数“up_votes”的某个地方期待一个“结束”,但我看不到在哪里?难道还有别的可能吗?

Up_votes 函数说明:

如果投票的值为 1,则该函数应该添加到数组中,如果为 -1,则不添加它。一旦该循环完成,则应该对该数组中的总票数(赞成票)求和。

错误输出:

/Users/user/.rvm/rubies/ruby-  2.2.1/lib/ruby/gems/2.2.0/gems/activesupport-  4.2.1/lib/active_support/dependencies.rb:274:in `require':     /Users/user/code/bloccit/app/models/post.rb:63: syntax error, unexpected end-of-input, expecting keyword_end (SyntaxError)

包含 up_votes 方法的 Post.rb 文件:

class Post < ActiveRecord::Base
 has_many :comments, dependent: :destroy
 has_many :votes
 has_one :summary
 belongs_to :user #means the post table has the user table's primary key in it
 belongs_to :topic
 mount_uploader :avatar, AvatarUploader
 default_scope {order('created_at DESC')}

 validates :title, length: {minimum: 5},  presence: true
 validates :body,  length: {minimum: 20}, presence: true



 def markdown_title
  (render_as_markdown).render(self.title).html_safe
 end


 def markdown_body
  (render_as_markdown).render(self.body).html_safe
 end

 def up_votes
  vote_array = []
  sum = 0
    vote_array = @post.votes.value.each do |vote| unless  @post.votes.value == -1
     vote_catch = vote_array
    end 
     vote_catch.each do |vote|
     sum += vote
    end 
     sum
  end
  end

Rspec 测试正在运行:

require 'rails_helper'

describe Post do
describe "vote methods" do

before do
  @post = Post.create(title: 'Post title', body: 'Post bodies must be pretty long.')
  3.times { @post.votes.create(value: 1)}
  2.times { @post.votes.create(value: -1)}
end

describe '#up_votes' do
  it "counts the number of votes with value = 1" do
    expect(@post.up_votes ).to eq(3)
  end
end

describe '#down_votes' do
  it "counts the number of votes with values = -1" do
    expect(@post.down_votes ).to eq(2)
  end
end

describe '#points' do
  it "returns the sum of all down and up votes" do
    expect(@post.points ).to eq(1) # 3 - 2
  end
end
end
end 

投票架构

create_table "votes", force: :cascade do |t|
 t.integer  "value"
 t.integer  "post_id"
 t.integer  "user_id"
 t.datetime "created_at", null: false
 t.datetime "updated_at", null: false
end

【问题讨论】:

  • 错误信息说你的post.rb模型至少有63行。您似乎没有发布整个文件。
  • 就是这样。其他行被注释掉。不知道为什么它说第 63 行。我会发布所有这些以防万一。

标签: ruby ruby-on-rails-4 rspec


【解决方案1】:

这可能只是您将代码放在这里的方式,但是...

vote_catch.each

不与它的“结束”内联。另外,up_votes 的“结束”与函数的结束不对齐。

编辑:

错误在于除非条件与#each 方法调用配对。 这是一个例子:

2.2.1 :008 > x.each do |vote| unless x == -1
2.2.1 :009?>     puts vote
2.2.1 :010?>     end
2.2.1 :011?>   end
0
1
2
3
4
5
...
10
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

【讨论】:

  • 不,不是这样。堆栈溢出中的代码很难对齐。
  • @SaintClaire33 你要结束整个课程吗?
  • 是的。 sum 之后的第二个结束是结束类。
  • @SaintClaire33 您需要为 unless 语句添加第二个结尾。将除非与块配对很奇怪。
  • 感谢@userFriendly 成功了。运行测试后出现另一个错误:1) 投票后方法 #up_votes 计算 value = 1 失败/错误的投票数:expect(@post.up_votes ).to eq(3) NoMethodError: undefined method votes' for nil:NilClass # ./app/models/post.rb:29:in up_votes ' # ./spec/models/post_spec.rb:14:in `block (4 levels) in '
猜你喜欢
  • 2023-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-10
  • 1970-01-01
  • 2017-06-25
相关资源
最近更新 更多