|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "spec_helper" |
| 4 | +require "fixturebot/syntax" |
| 5 | + |
| 6 | +RSpec.describe FixtureBot::Syntax::Methods do |
| 7 | + let(:test_context) do |
| 8 | + Object.new.tap do |ctx| |
| 9 | + ctx.extend(FixtureBot::Syntax::Methods) |
| 10 | + |
| 11 | + # Stub fixture accessor: users(:brad), users(:alice) |
| 12 | + brad = double("brad", |
| 13 | + id: 1, |
| 14 | + attributes: { "id" => 1, "name" => "Brad", "email" => "brad@example.com", "created_at" => "2024-01-01", "updated_at" => "2024-01-01" }, |
| 15 | + class: user_class |
| 16 | + ) |
| 17 | + allow(brad).to receive(:dup).and_return( |
| 18 | + double("brad_dup", |
| 19 | + id: nil, |
| 20 | + new_record?: true, |
| 21 | + persisted?: false, |
| 22 | + class: user_class |
| 23 | + ).tap do |dup| |
| 24 | + allow(dup).to receive(:assign_attributes) |
| 25 | + allow(dup).to receive(:save!) |
| 26 | + end |
| 27 | + ) |
| 28 | + |
| 29 | + alice = double("alice", |
| 30 | + id: 2, |
| 31 | + attributes: { "id" => 2, "name" => "Alice", "email" => "alice@example.com", "created_at" => "2024-01-01", "updated_at" => "2024-01-01" }, |
| 32 | + class: user_class |
| 33 | + ) |
| 34 | + allow(alice).to receive(:dup).and_return( |
| 35 | + double("alice_dup", |
| 36 | + id: nil, |
| 37 | + new_record?: true, |
| 38 | + persisted?: false, |
| 39 | + class: user_class |
| 40 | + ).tap do |dup| |
| 41 | + allow(dup).to receive(:assign_attributes) |
| 42 | + allow(dup).to receive(:save!) |
| 43 | + end |
| 44 | + ) |
| 45 | + |
| 46 | + allow(ctx).to receive(:users).with(:brad).and_return(brad) |
| 47 | + allow(ctx).to receive(:users).with(:alice).and_return(alice) |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + let(:user_class) do |
| 52 | + klass = double("User") |
| 53 | + allow(klass).to receive(:instantiate) do |attrs| |
| 54 | + double("stubbed_user", id: attrs["id"], persisted?: true, new_record?: false, **attrs.transform_keys(&:to_sym)) |
| 55 | + end |
| 56 | + klass |
| 57 | + end |
| 58 | + |
| 59 | + describe "#build" do |
| 60 | + it "returns a dup of the fixture record" do |
| 61 | + result = test_context.build(:user, :brad) |
| 62 | + expect(result.new_record?).to be true |
| 63 | + end |
| 64 | + |
| 65 | + it "applies attribute overrides" do |
| 66 | + result = test_context.build(:user, :brad, name: "Changed") |
| 67 | + expect(result).to have_received(:assign_attributes).with(name: "Changed") |
| 68 | + end |
| 69 | + |
| 70 | + it "does not call assign_attributes when no overrides given" do |
| 71 | + result = test_context.build(:user, :brad) |
| 72 | + expect(result).not_to have_received(:assign_attributes) |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + describe "#create" do |
| 77 | + it "returns the original fixture when no overrides given" do |
| 78 | + result = test_context.create(:user, :brad) |
| 79 | + expect(result.id).to eq(1) |
| 80 | + end |
| 81 | + |
| 82 | + it "builds and saves when overrides are given" do |
| 83 | + result = test_context.create(:user, :brad, name: "Changed") |
| 84 | + expect(result).to have_received(:assign_attributes).with(name: "Changed") |
| 85 | + expect(result).to have_received(:save!) |
| 86 | + end |
| 87 | + end |
| 88 | + |
| 89 | + describe "#build_stubbed" do |
| 90 | + it "retains the id from the fixture" do |
| 91 | + result = test_context.build_stubbed(:user, :brad) |
| 92 | + expect(result.id).to eq(1) |
| 93 | + end |
| 94 | + |
| 95 | + it "uses model class instantiate" do |
| 96 | + test_context.build_stubbed(:user, :brad) |
| 97 | + expect(user_class).to have_received(:instantiate) |
| 98 | + end |
| 99 | + |
| 100 | + it "merges attribute overrides" do |
| 101 | + result = test_context.build_stubbed(:user, :brad, name: "Stubbed") |
| 102 | + expect(result.name).to eq("Stubbed") |
| 103 | + end |
| 104 | + end |
| 105 | + |
| 106 | + describe "#attributes_for" do |
| 107 | + it "returns a hash without id, created_at, updated_at" do |
| 108 | + result = test_context.attributes_for(:user, :brad) |
| 109 | + expect(result).to eq({ name: "Brad", email: "brad@example.com" }) |
| 110 | + end |
| 111 | + |
| 112 | + it "merges attribute overrides" do |
| 113 | + result = test_context.attributes_for(:user, :brad, name: "Override") |
| 114 | + expect(result[:name]).to eq("Override") |
| 115 | + end |
| 116 | + end |
| 117 | + |
| 118 | + describe "#build_list" do |
| 119 | + it "returns a record for each fixture name" do |
| 120 | + results = test_context.build_list(:user, :brad, :alice) |
| 121 | + expect(results.length).to eq(2) |
| 122 | + expect(results).to all(be_truthy) |
| 123 | + end |
| 124 | + end |
| 125 | + |
| 126 | + describe "#create_list" do |
| 127 | + it "returns a record for each fixture name" do |
| 128 | + results = test_context.create_list(:user, :brad, :alice) |
| 129 | + expect(results.length).to eq(2) |
| 130 | + end |
| 131 | + end |
| 132 | + |
| 133 | + describe "#build_pair" do |
| 134 | + it "returns two records" do |
| 135 | + results = test_context.build_pair(:user, :brad, :alice) |
| 136 | + expect(results.length).to eq(2) |
| 137 | + end |
| 138 | + end |
| 139 | + |
| 140 | + describe "#create_pair" do |
| 141 | + it "returns two records" do |
| 142 | + results = test_context.create_pair(:user, :brad, :alice) |
| 143 | + expect(results.length).to eq(2) |
| 144 | + end |
| 145 | + end |
| 146 | + |
| 147 | + describe "#build_stubbed_list" do |
| 148 | + it "returns stubbed records for each fixture name" do |
| 149 | + results = test_context.build_stubbed_list(:user, :brad, :alice) |
| 150 | + expect(results.length).to eq(2) |
| 151 | + expect(results.first.id).to eq(1) |
| 152 | + expect(results.last.id).to eq(2) |
| 153 | + end |
| 154 | + end |
| 155 | + |
| 156 | + describe "#build_stubbed_pair" do |
| 157 | + it "returns two stubbed records" do |
| 158 | + results = test_context.build_stubbed_pair(:user, :brad, :alice) |
| 159 | + expect(results.length).to eq(2) |
| 160 | + end |
| 161 | + end |
| 162 | +end |
0 commit comments