0
我的rspec測試vote__spec.rb指出我的up_votes方法未定義,但它在列出的votes.rb文件中列出。任何人都可以指導我爲什麼說它是未定義的?Rspec測試無法在模型文件中找到方法
Rspec的錯誤
2) Vote validations value validation only allows -1 or 1 as values
Failure/Error: expect(@votes.up_votes).where(value: [1,-1])
NoMethodError:
undefined method `up_votes' for nil:NilClass
# ./spec/models/vote_spec.rb:5:in `block (4 levels) in <top (required)>'
投票Rspec的
describe Vote do
describe "validations" do
describe "value validation" do
it "only allows -1 or 1 as values" do
expect(@post.up_votes).where(value: [1,-1])
end
end
end
end
Vote.rb文件
class Vote < ActiveRecord::Base
belongs_to :user
belongs_to :post
def up_votes
votes.where(value: 1).count
end
end