Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 运行rspec测试时出现意外的关键字错误_Ruby_Ruby On Rails 4_Rspec - Fatal编程技术网

Ruby 运行rspec测试时出现意外的关键字错误

Ruby 运行rspec测试时出现意外的关键字错误,ruby,ruby-on-rails-4,rspec,Ruby,Ruby On Rails 4,Rspec,全部, 运行我的rspec测试时,脚本在运行测试之前失败。看起来它期待在函数“up_vots”的某个地方出现“end”,但我看不出在哪里?可能还有别的事吗 向上投票功能说明: /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/bloc

全部,

运行我的rspec测试时,脚本在运行测试之前失败。看起来它期待在函数“up_vots”的某个地方出现“end”,但我看不出在哪里?可能还有别的事吗

向上投票功能说明:

/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)
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
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 
如果投票的值为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)
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
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 
Post.rb文件,其中包含向上投票方法:

/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)
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
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

这可能就是你把代码放在这里的方式,但是

vote_catch.each
与其“结束”不一致。此外,up_投票的“结束”与函数的结束不一致

编辑:

错误是除非条件与#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]

不,不是这样。堆栈溢出中的代码很难对齐。@E33是否结束整个类?是的。sum之后的第二个结尾是类的结尾。@SaintClaire33除非语句需要第二个结尾。将除非与块配对很奇怪。谢谢@userFriendly,这很有效。运行测试后,我遇到了另一个错误:1)投票后方法(Post-vote methods)#up_vots统计值为1的投票数失败/错误:expect(@Post.up_vots)。到等式(3)NoMethodError:nil:NilClass的未定义方法
vots./app/models/Post.rb:29:in
up#/spec./spec/models/Post#.rb:14:in`block(4级)在“错误消息”中说您的
post.rb
模型至少有63行。似乎您没有发布整个文件。仅此而已。其他行已注释掉。不确定为什么会显示第63行。我将发布所有行,以防万一。