Ruby on rails 试图理解为什么验证测试的存在失败
我刚刚开始玩Rails(使用Rspec和Shoulda Matchers)来构建一个演示博客 我刚刚开始,第一次考试不及格,但我不明白为什么 我认为我已经正确地设置了所有内容,但是当我尝试验证文章模型上是否存在标题时,它返回一个失败Ruby on rails 试图理解为什么验证测试的存在失败,ruby-on-rails,rspec,shoulda,Ruby On Rails,Rspec,Shoulda,我刚刚开始玩Rails(使用Rspec和Shoulda Matchers)来构建一个演示博客 我刚刚开始,第一次考试不及格,但我不明白为什么 我认为我已经正确地设置了所有内容,但是当我尝试验证文章模型上是否存在标题时,它返回一个失败 Shoulda::Matchers::ActiveModel::AllowValueMatcher::AttributeDoesNotExistError: The matcher attempted to set :title on the Article
Shoulda::Matchers::ActiveModel::AllowValueMatcher::AttributeDoesNotExistError:
The matcher attempted to set :title on the Article to nil, but that
attribute does not exist.
我的模型看起来像这样
class Article < ApplicationRecord
# attr_accessor :title
validates_presence_of :title
end
如果我取消对attr_访问器的注释,那么测试就通过了,但我知道Rails并不需要它
我做错了什么?只要您的
文章
数据库表有标题
列,就不需要属性访问器
您可以在
db/schema.rb
完美感谢@hugh middleton,我已经更新了迁移文件,但没有意识到模式。不确定从哪里读取属性。不用担心。是的,只要您确实在运行迁移,schema.rb
就会自动更新。
require 'rails_helper'
RSpec.describe Article do
it { should validate_presence_of :title }
end