简体   繁体   中英

How can I use more than one set of fixtures per table in Rails?

I want to define alternative sets of fixtures per database table.

For example,

class BusinessSpec
  describe "some aspect of businesses" do
    fixtures :fixture_set_1

    ...
  end

  describe "some unrelated aspect of businesses" do
    fixtures :fixture_set_2
    ...
  end
end

Is this, or something similar, possible?

It's much easier to do this using something like factory_girl . You can define multiple factories per table.

Factory.define :my_post, :class => Post do |f|
  f.name "Mine"
  f.created_at 2.weeks.ago
end

Factory.define :another_post, :class => Post, :parent => :my_post do |f|
  f.name "Another"
end

The 2nd example keeps things dry. Factory girl is an easy replacement for fixtures. Try it, you'll love it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM