模型规范
模型规范由 `type: :model` 标记,或者如果您已设置 `config.infer_spec_type_from_file_location!`,则将它们放在 `spec/models` 中。
模型规范是 ActiveSupport::TestCase 的一个薄包装器,除了 RSpec 的自身行为和预期之外,它还包含了 ActiveSupport::TestCase 提供的所有行为和断言。
示例
require "rails_helper"
RSpec.describe Post, type: :model do
context "with 2 or more comments" do
it "orders them in reverse chronologically" do
post = Post.create!
comment1 = post.comments.create!(:body => "first comment")
comment2 = post.comments.create!(:body => "second comment")
expect(post.reload.comments).to eq([comment2, comment1])
end
end
end