fixturebot-rails 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +68 -0
- data/lib/fixturebot/minitest.rb +3 -0
- data/lib/fixturebot/rspec.rb +3 -0
- data/lib/fixturebot/syntax.rb +63 -0
- data/lib/fixturebot/version.rb +1 -1
- data/lib/fixturebot.rb +4 -0
- data/lib/generators/fixturebot/templates/fixtures.rb.tt +2 -0
- metadata +3 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 8c83c05f009409eb651e9e464fcc70bad3b3abeb377943eef42bfff31b29f9ac
|
|
4
|
+
data.tar.gz: 79973f2828cb2dcb83c4f238ad1023681c0a241d0f7ca1daab13ad1627685f64
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b3c5cfd9eeb975bb9209a2a3e37ca98d7a00225b6c63f638c3f4bdc2a44ff2782eebfcc4616a327d1134d3bcf3dcbbc0a5a661918ab1f4b6fefdffc4ebefed96
|
|
7
|
+
data.tar.gz: 3ea33f5090363d4ee7c8be75720e7fa1a8364c7665304824071d08fd2e5912be7f0d293f9255c7c69f61a0bafc9c06d9ca600954e33d6adb321dba1a748b10eb
|
data/README.md
CHANGED
|
@@ -346,6 +346,40 @@ end
|
|
|
346
346
|
|
|
347
347
|
In Rails, this is auto-detected from the database.
|
|
348
348
|
|
|
349
|
+
### Multiple files
|
|
350
|
+
|
|
351
|
+
For larger apps, split fixtures across multiple files using `FixtureBot.require`:
|
|
352
|
+
|
|
353
|
+
```ruby
|
|
354
|
+
# spec/fixtures.rb
|
|
355
|
+
FixtureBot.require "spec/fixtures/**/*.rb"
|
|
356
|
+
|
|
357
|
+
FixtureBot.define do
|
|
358
|
+
user.email { |fixture| "#{fixture.key}@example.com" }
|
|
359
|
+
end
|
|
360
|
+
```
|
|
361
|
+
|
|
362
|
+
```ruby
|
|
363
|
+
# spec/fixtures/users.rb
|
|
364
|
+
FixtureBot.define do
|
|
365
|
+
user :brad do
|
|
366
|
+
name "Brad"
|
|
367
|
+
end
|
|
368
|
+
end
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
```ruby
|
|
372
|
+
# spec/fixtures/posts.rb
|
|
373
|
+
FixtureBot.define do
|
|
374
|
+
post :hello do
|
|
375
|
+
title "Hello"
|
|
376
|
+
author :brad
|
|
377
|
+
end
|
|
378
|
+
end
|
|
379
|
+
```
|
|
380
|
+
|
|
381
|
+
Each file calls `FixtureBot.define` with its own block. Files are loaded in alphabetical order. References across files work because everything is resolved after all files are loaded.
|
|
382
|
+
|
|
349
383
|
### Implicit vs explicit style
|
|
350
384
|
|
|
351
385
|
By default, the block is evaluated implicitly. Table methods like `user` and `post` are available directly:
|
|
@@ -388,6 +422,40 @@ FixtureBot::Schema.define do
|
|
|
388
422
|
end
|
|
389
423
|
```
|
|
390
424
|
|
|
425
|
+
## Migrating from FactoryBot
|
|
426
|
+
|
|
427
|
+
FixtureBot provides `build`, `create`, `attributes_for`, and related methods that mirror FactoryBot's API. The key difference is that you pass both a table name and a fixture name instead of just a factory name:
|
|
428
|
+
|
|
429
|
+
```ruby
|
|
430
|
+
# FactoryBot # FixtureBot
|
|
431
|
+
build(:user) # build(:user, :brad)
|
|
432
|
+
create(:user) # create(:user, :brad)
|
|
433
|
+
build(:user, name: "X") # build(:user, :brad, name: "X")
|
|
434
|
+
attributes_for(:user) # attributes_for(:user, :brad)
|
|
435
|
+
build_list(:user, 3) # build_list(:user, :brad, :alice, :bob)
|
|
436
|
+
create_list(:user, 3) # create_list(:user, :brad, :alice, :bob)
|
|
437
|
+
build_pair(:user) # build_pair(:user, :brad, :alice)
|
|
438
|
+
create_pair(:user) # create_pair(:user, :brad, :alice)
|
|
439
|
+
build_stubbed(:user) # build_stubbed(:user, :brad)
|
|
440
|
+
```
|
|
441
|
+
|
|
442
|
+
### Method reference
|
|
443
|
+
|
|
444
|
+
| Method | Behavior |
|
|
445
|
+
|---|---|
|
|
446
|
+
| `build(:user, :brad, **attrs)` | Duplicates the fixture, applies overrides. Returns unpersisted. |
|
|
447
|
+
| `create(:user, :brad, **attrs)` | Without overrides: returns the fixture (already persisted). With overrides: `build` + `save!`. |
|
|
448
|
+
| `build_stubbed(:user, :brad, **attrs)` | Like `build` but retains `id`. Looks persisted without touching DB. |
|
|
449
|
+
| `attributes_for(:user, :brad, **attrs)` | Returns attributes hash, strips `id`/`created_at`/`updated_at`. |
|
|
450
|
+
| `build_list(:user, :brad, :alice, **attrs)` | Maps each name through `build`. |
|
|
451
|
+
| `create_list(:user, :brad, :alice, **attrs)` | Maps each name through `create`. |
|
|
452
|
+
| `build_pair(:user, :brad, :alice, **attrs)` | Alias for `build_list` with 2 names. |
|
|
453
|
+
| `create_pair(:user, :brad, :alice, **attrs)` | Alias for `create_list` with 2 names. |
|
|
454
|
+
| `build_stubbed_list(:user, :brad, :alice, **attrs)` | Maps each name through `build_stubbed`. |
|
|
455
|
+
| `build_stubbed_pair(:user, :brad, :alice, **attrs)` | Alias for `build_stubbed_list` with 2 names. |
|
|
456
|
+
|
|
457
|
+
These methods are automatically available in your tests when you require `fixturebot/rspec` or `fixturebot/minitest`. They call the standard Rails fixture accessors under the hood, so `build(:user, :brad)` is equivalent to `users(:brad).dup`.
|
|
458
|
+
|
|
391
459
|
## Prior art
|
|
392
460
|
|
|
393
461
|
### [Rails fixtures](https://guides.rubyonrails.org/testing.html#the-low-down-on-fixtures)
|
data/lib/fixturebot/minitest.rb
CHANGED
data/lib/fixturebot/rspec.rb
CHANGED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "active_support/core_ext/string/inflections"
|
|
4
|
+
require "active_support/core_ext/hash/keys"
|
|
5
|
+
|
|
6
|
+
module FixtureBot
|
|
7
|
+
module Syntax
|
|
8
|
+
module Methods
|
|
9
|
+
def build(table_name, fixture_name, **attributes)
|
|
10
|
+
record = send(table_name.to_s.pluralize, fixture_name).dup
|
|
11
|
+
record.assign_attributes(attributes) if attributes.any?
|
|
12
|
+
record
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def create(table_name, fixture_name, **attributes)
|
|
16
|
+
if attributes.any?
|
|
17
|
+
build(table_name, fixture_name, **attributes).tap(&:save!)
|
|
18
|
+
else
|
|
19
|
+
send(table_name.to_s.pluralize, fixture_name)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def build_stubbed(table_name, fixture_name, **attributes)
|
|
24
|
+
source = send(table_name.to_s.pluralize, fixture_name)
|
|
25
|
+
attrs = source.attributes
|
|
26
|
+
attrs.merge!(attributes.stringify_keys) if attributes.any?
|
|
27
|
+
source.class.instantiate(attrs)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def attributes_for(table_name, fixture_name, **attributes)
|
|
31
|
+
send(table_name.to_s.pluralize, fixture_name)
|
|
32
|
+
.attributes
|
|
33
|
+
.symbolize_keys
|
|
34
|
+
.except(:id, :created_at, :updated_at)
|
|
35
|
+
.merge(attributes)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def build_list(table_name, *fixture_names, **attributes)
|
|
39
|
+
fixture_names.map { |name| build(table_name, name, **attributes) }
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def create_list(table_name, *fixture_names, **attributes)
|
|
43
|
+
fixture_names.map { |name| create(table_name, name, **attributes) }
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def build_pair(table_name, *fixture_names, **attributes)
|
|
47
|
+
build_list(table_name, *fixture_names, **attributes)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def create_pair(table_name, *fixture_names, **attributes)
|
|
51
|
+
create_list(table_name, *fixture_names, **attributes)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def build_stubbed_list(table_name, *fixture_names, **attributes)
|
|
55
|
+
fixture_names.map { |name| build_stubbed(table_name, name, **attributes) }
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def build_stubbed_pair(table_name, *fixture_names, **attributes)
|
|
59
|
+
build_stubbed_list(table_name, *fixture_names, **attributes)
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
data/lib/fixturebot/version.rb
CHANGED
data/lib/fixturebot.rb
CHANGED
|
@@ -29,6 +29,10 @@ module FixtureBot
|
|
|
29
29
|
end
|
|
30
30
|
end
|
|
31
31
|
|
|
32
|
+
def self.require(glob)
|
|
33
|
+
Dir.glob(glob).sort.each { |f| load f }
|
|
34
|
+
end
|
|
35
|
+
|
|
32
36
|
def self.define_from_file(schema, fixtures_path)
|
|
33
37
|
definition = Definition.new(schema)
|
|
34
38
|
Thread.current[:fixturebot_definition] = definition
|
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: fixturebot-rails
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Brad Gessler
|
|
8
8
|
bindir: exe
|
|
9
9
|
cert_chain: []
|
|
10
|
-
date: 2026-03-
|
|
10
|
+
date: 2026-03-11 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
12
12
|
- !ruby/object:Gem::Dependency
|
|
13
13
|
name: activerecord
|
|
@@ -85,6 +85,7 @@ files:
|
|
|
85
85
|
- lib/fixturebot/row.rb
|
|
86
86
|
- lib/fixturebot/rspec.rb
|
|
87
87
|
- lib/fixturebot/schema.rb
|
|
88
|
+
- lib/fixturebot/syntax.rb
|
|
88
89
|
- lib/fixturebot/version.rb
|
|
89
90
|
- lib/generators/fixturebot/install_generator.rb
|
|
90
91
|
- lib/generators/fixturebot/templates/fixtures.rb.tt
|