diff options
author | Tongfei Gao <gaotongfei1995@gmail.com> | 2019-05-18 20:58:22 +0800 |
---|---|---|
committer | Tongfei Gao <gaotongfei1995@gmail.com> | 2019-07-27 16:40:16 +0800 |
commit | c09a4fd23d7d52552132edde7ad155f366c560b8 (patch) | |
tree | 689a66272dffccef08230af259a687ed4be960cd /activerecord/test | |
parent | d1ffe59ab5fd6e811833c127d43b32e87b5d7131 (diff) | |
download | rails-c09a4fd23d7d52552132edde7ad155f366c560b8.tar.gz rails-c09a4fd23d7d52552132edde7ad155f366c560b8.tar.bz2 rails-c09a4fd23d7d52552132edde7ad155f366c560b8.zip |
Allow specify fixtures to be ignored
Allow specifying what fixtures can be ignored by setting
`ignore` in fixtures YAML file:
# users.yml
_fixture:
ignore:
- base
base: &base
admin: false
introduction: "This is a default description"
admin:
<<: *base
admin: true
visitor:
<<: *base
In the above example, "base" fixture will be ignored when creating
users fixture. This is helpful when you want to inherit attributes
and it makes your fixtures more "DRY".
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/fixtures_test.rb | 27 | ||||
-rw-r--r-- | activerecord/test/fixtures/other_books.yml | 26 | ||||
-rw-r--r-- | activerecord/test/fixtures/parrots.yml | 8 |
3 files changed, 60 insertions, 1 deletions
diff --git a/activerecord/test/cases/fixtures_test.rb b/activerecord/test/cases/fixtures_test.rb index a7f01e898e..7ad032a632 100644 --- a/activerecord/test/cases/fixtures_test.rb +++ b/activerecord/test/cases/fixtures_test.rb @@ -1279,6 +1279,33 @@ class CustomNameForFixtureOrModelTest < ActiveRecord::TestCase end end +class IgnoreFixturesTest < ActiveRecord::TestCase + fixtures :other_books, :parrots + + test "ignores books fixtures" do + assert_raise(StandardError) { other_books(:published) } + assert_raise(StandardError) { other_books(:published_paperback) } + assert_raise(StandardError) { other_books(:published_ebook) } + + assert_equal 2, Book.count + assert_equal "Agile Web Development with Rails", other_books(:awdr).name + assert_equal "published", other_books(:awdr).status + assert_equal "paperback", other_books(:awdr).format + assert_equal "english", other_books(:awdr).language + + assert_equal "Ruby for Rails", other_books(:rfr).name + assert_equal "ebook", other_books(:rfr).format + assert_equal "published", other_books(:rfr).status + end + + test "ignores parrots fixtures" do + assert_raise(StandardError) { parrots(:DEFAULT) } + assert_raise(StandardError) { parrots(:DEAD_PARROT) } + + assert_equal "DeadParrot", parrots(:polly).parrot_sti_class + end +end + class FixturesWithDefaultScopeTest < ActiveRecord::TestCase fixtures :bulbs diff --git a/activerecord/test/fixtures/other_books.yml b/activerecord/test/fixtures/other_books.yml new file mode 100644 index 0000000000..62806c03d7 --- /dev/null +++ b/activerecord/test/fixtures/other_books.yml @@ -0,0 +1,26 @@ +_fixture: + model_class: Book + ignore: + - PUBLISHED + - PUBLISHED_PAPERBACK + - PUBLISHED_EBOOK + +PUBLISHED: &PUBLISHED + status: :published + +PUBLISHED_PAPERBACK: &PUBLISHED_PAPERBACK + <<: *PUBLISHED + format: "paperback" + language: :english + +PUBLISHED_EBOOK: &PUBLISHED_EBOOK + <<: *PUBLISHED + format: "ebook" + +awdr: + <<: *PUBLISHED_PAPERBACK + name: "Agile Web Development with Rails" + +rfr: + <<: *PUBLISHED_EBOOK + name: "Ruby for Rails" diff --git a/activerecord/test/fixtures/parrots.yml b/activerecord/test/fixtures/parrots.yml index 8425ef98e0..4f0a090e03 100644 --- a/activerecord/test/fixtures/parrots.yml +++ b/activerecord/test/fixtures/parrots.yml @@ -1,3 +1,9 @@ +_fixture: + ignore: DEAD_PARROT + +DEAD_PARROT: &DEAD_PARROT + parrot_sti_class: DeadParrot + george: name: "Curious George" treasures: diamond, sapphire @@ -17,7 +23,7 @@ polly: name: $LABEL killer: blackbeard treasures: sapphire, ruby - parrot_sti_class: DeadParrot + <<: *DEAD_PARROT DEFAULTS: &DEFAULTS treasures: sapphire, ruby |