File: nested_attributes_spec.rb

package info (click to toggle)
ruby-factory-bot 6.5.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,492 kB
  • sloc: ruby: 9,242; makefile: 6; sh: 4
file content (32 lines) | stat: -rw-r--r-- 880 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
describe "association assignment from nested attributes" do
  before do
    define_model("Post", title: :string) do
      has_many :comments
      accepts_nested_attributes_for :comments
    end

    define_model("Comment", post_id: :integer, body: :text) do
      belongs_to :post
    end

    FactoryBot.define do
      factory :post do
        comments_attributes { [FactoryBot.attributes_for(:comment), FactoryBot.attributes_for(:comment)] }
      end

      factory :comment do
        sequence(:body) { |n| "Body #{n}" }
      end
    end
  end

  it "assigns the correct amount of comments" do
    expect(FactoryBot.create(:post).comments.count).to eq 2
  end

  it "assigns the correct amount of comments when overridden" do
    post = FactoryBot.create(:post, comments_attributes: [FactoryBot.attributes_for(:comment)])

    expect(post.comments.count).to eq 1
  end
end