运行rspec test

时间:2015-07-04 14:42:45

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

所有

运行我的rspec测试时,我收到以下未定义的方法错误。当我在方法拼写错误或我的执行顺序导致它之前得到此错误时。我在StackOverflow上查看了其他一些帖子,但没有任何帮助。任何人都可以提供任何指导吗?

Rspec失败:

FFFF

Failures:

1) Post vote methods #up_votes counts the number of votes with value = 1
 Failure/Error: expect(@post.up_votes ).to eq(3)
 NoMethodError:
   undefined method `up_votes' for #<Post:0x007fe92f381a38>
 # ./spec/models/post_spec.rb:14:in `block (4 levels) in <top (required)>'

 2) Post vote methods #down_votes counts the number of votes with values = -1
 Failure/Error: expect(@post.down_votes ).to eq(2)
 NoMethodError:
   undefined method `down_votes' for #<Post:0x007fe92a18c228>
 # ./spec/models/post_spec.rb:20:in `block (4 levels) in <top (required)>'

 3) Post vote methods #points returns the sum of all down and up votes
 Failure/Error: expect(@post.points ).to eq(1) # 3 - 2
 NoMethodError:
   undefined method `points' for #<Post:0x007fe92986c620>
 # ./spec/models/post_spec.rb:26:in `block (4 levels) in <top (required)>'

 4) Vote validations value validation only allows -1 or 1 as values
 Failure/Error: expect(@post.up_votes).to eq((-1) || eq(1))
 NoMethodError:
   undefined method `up_votes' for nil:NilClass
 # ./spec/models/vote_spec.rb:5:in `block (4 levels) in <top (required)>'

Post_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 

vote_spec文件

describe Vote do
 describe "validations" do
  describe "value validation" do
  it "only allows -1 or 1 as values" do
    expect(@post.up_votes).to eq((-1) || eq(1)) 
  end
 end
 end
 end

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

private

def render_as_markdown
renderer = Redcarpet::Render::HTML.new
extensions = {fenced_code_blocks: true}
redcarpet = Redcarpet::Markdown.new(renderer, extensions)
#return redcarpet
 end


 end

1 个答案:

答案 0 :(得分:0)

对于post_spec.rb错误,请检查Post模型(请参阅文件app/models/post.rb),其中定义了以下方法:

  • up_votes
  • down_votes
  • points

请考虑在原始问题中加入post.rb的代码。

对于vote_spec.rb错误,没有@post变量,它将是nil。此错误消息提示:

Failure/Error: expect(@post.up_votes).to eq((-1) || eq(1))
NoMethodError: undefined method `up_votes' for nil:NilClass