【发布时间】:2017-06-04 11:58:46
【问题描述】:
我刚刚开始使用 Rails(使用 Rspec 和 Shoulda Matchers)来构建一个演示博客。
我刚刚开始,我的第一个测试失败了,但我不明白为什么。
我认为我已经正确设置了所有内容,但是当我尝试验证我的文章模型上是否存在标题但它返回失败时
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
还有我的测试...
require 'rails_helper'
RSpec.describe Article do
it { should validate_presence_of :title }
end
如果我取消注释 attr_accessor 则测试通过,但我知道 Rails 不需要它。
我做错了什么?
【问题讨论】:
标签: ruby-on-rails rspec shoulda