我在实现Rspec时遇到了一些问题。我有三个模型;
Post
,则,
Tagging
和
Tag
。
应用程序/型号/标签。rb
class Tag < ApplicationRecord
# associations
has_many :taggings
has_many :posts, through: :taggings
# validations
validates :name, presence: true, uniqueness: { case_sensitive: false }
# returns a list of posts that are belonging to tag.
def posts
...
end
end
我能够为关联和验证编写规范,但仍坚持为的实例方法编写规范
def posts ... end
.有人能简要解释一下如何编写此规范吗?我是Rspec的新手,所以请容忍我。
规格/型号/tag\u spec.rb
require 'rails_helper'
RSpec.describe Tag, type: :model do
describe "Associations" do
it { should have_many(:posts).through(:taggings) }
end
describe "Validations" do
subject { FactoryBot.create(:tag) }
it { should validate_presence_of(:name) }
it { should validate_uniqueness_of(:name).case_insensitive }
end
describe "#posts" do
# need help
end
end