From Fixture API docs :
This type of fixture is in YAML format and the preferred default. YAML is a file format which describes data structures in a non-verbose, humanly-readable format. It ships with Ruby 1.8.1+.
Unlike single-file fixtures, YAML fixtures are stored in a single file per model, which is placed in the directory appointed by Test::Unit::TestCase.fixture_path=(path) (this is automatically configured for Rails, so you can just put your files in <your-rails-app>/test/fixtures/web_sites.yml“). The format of a YAML fixture file looks like this:
rubyonrails:
id: 1
name: Ruby on Rails
url: <a href="http://www.rubyonrails.org">http://www.rubyonrails.org</a>
google:
id: 2
name: Google
url: <a href="http://www.google.com">http://www.google.com</a>
This YAML fixture file includes two fixtures. Each YAML fixture (ie. record) is given a name and is followed by an indented list of key/value pairs in the “key: value” format. Records are separated by a blank line for your viewing pleasure.
See the API Docs for Fixtures for more details on how to use YAML fixtures in your test.
YAML fixtures are available as of Rails 0.9
Use my ActiveRecord plugin to automatically generate YAML fixtures from data in your database. You can then modify the generated fixtures to meet your specific needs.
Question: is there a way to insert arbitrary Ruby code into a test fixture, and have it evaluated? (e.g. I want to assign Time.now to certain timestamp field)
—AlexeyVerkhovsky
You can now use erb in your fixture files, so
<%= Time.now %>is absolutely valid.
Though you will have much better luck with
<%=Time.now.strftime("%Y-%m-%d %H:%M:%S")%>
-mml
I found this on Scott Raymond’s blog: Time.now.to_s(:db)
that does the yaml formating.
Question: How do I use Fixture to test Class that belongs_to other Class because if I just add ‘fixtures :parent’ to my \ChildTest class. I will get data integrity error from DB when try to insert that Fixture into DB.
Make sure the fixture for the child class has the proper id of the parent class specified. When you specify the fixtures to use in your test place them in order of dependence.Example:
class Asset belongs_to :projectin assets.yml
first_asset: id: 1 project_id: 1 # assumes projects.yml has a project with id: 1in your test file:
fixtures :projects, :assetsnotice project is first. also, im not sure if it should be trusted that fixture load order should be the same as the specified order in the test file. so …
Question: could anyone look at Fixture code for 0.10.1? It seems to be so buggy compare to 0.9.5 I had. The code seems to be out of sync in how to use another code
Question: How do I keep the values true and false from turning into 1 and 0?
According to the YAML specs(http://www.yaml.org/refcard.html) “Yes”, “yes”, “true”, 1 are all counted as 1. Same with false and friends.The solution is quite simple. In your YAML file, quote the string like this:
record: field: "true"And the value should be the string “true” rather than 1.
Question: How to I create fixtures for the relationship between two models that have_and_belongs_to each other?
Question: Since rails 0.14, how can I programmatically enumerate all of the objects in a fixture, without having to name them specifically?
Question: How can I enable the yml or csv fixtures to be interpretted as unicode files? I currently want to insert a chinese character as sample data and although the application works from front to back with this data in mysql, I can’t load it as a part of my tests :( Any help is appreciated.
Question: Is there an easy way to use fixtures outside of rails?
Question: Is there a way to force fixtures to load in a certain order? eg the following won’t work in a rake task unless users is already loaded…
friends1:
user_id1: <%= User.find_by_nickname("alan") %>
user_id2: <%= User.find_by_nickname("bob") %>
Question: Is there a way to reference related records in a way like this:
people.yml
bob:
id: 1
name: Bob the builder
jobs.yml
builder:
id: 1
name: Construction worker/builder
jobs_users.yml
employment_bob:
person_id: bob # or people.bob
job_id: builder # or jobs.builder
.bq Since Rails 2, fixture-engine supports autogenerated, hashed, consistent IDs as well as label references between fixtures (through their associations). —gbence
Question: And to complete this kind of fixturing, is there a way to autoincrement or autogenerate id’s, since they are not of semantic value to my models. (I now use:)
<% i=0 %>
first_item:
id: <%= (i=i+1) %>
Just leave them out. The database will fill them in for you.
From Fixture API docs :
This type of fixture is in YAML format and the preferred default. YAML is a file format which describes data structures in a non-verbose, humanly-readable format. It ships with Ruby 1.8.1+.
Unlike single-file fixtures, YAML fixtures are stored in a single file per model, which is placed in the directory appointed by Test::Unit::TestCase.fixture_path=(path) (this is automatically configured for Rails, so you can just put your files in <your-rails-app>/test/fixtures/web_sites.yml“). The format of a YAML fixture file looks like this:
rubyonrails:
id: 1
name: Ruby on Rails
url: <a href="http://www.rubyonrails.org">http://www.rubyonrails.org</a>
google:
id: 2
name: Google
url: <a href="http://www.google.com">http://www.google.com</a>
This YAML fixture file includes two fixtures. Each YAML fixture (ie. record) is given a name and is followed by an indented list of key/value pairs in the “key: value” format. Records are separated by a blank line for your viewing pleasure.
See the API Docs for Fixtures for more details on how to use YAML fixtures in your test.
YAML fixtures are available as of Rails 0.9
Use my ActiveRecord plugin to automatically generate YAML fixtures from data in your database. You can then modify the generated fixtures to meet your specific needs.
Question: is there a way to insert arbitrary Ruby code into a test fixture, and have it evaluated? (e.g. I want to assign Time.now to certain timestamp field)
—AlexeyVerkhovsky
You can now use erb in your fixture files, so
<%= Time.now %>is absolutely valid.
Though you will have much better luck with
<%=Time.now.strftime("%Y-%m-%d %H:%M:%S")%>
-mml
I found this on Scott Raymond’s blog: Time.now.to_s(:db)
that does the yaml formating.
Question: How do I use Fixture to test Class that belongs_to other Class because if I just add ‘fixtures :parent’ to my \ChildTest class. I will get data integrity error from DB when try to insert that Fixture into DB.
Make sure the fixture for the child class has the proper id of the parent class specified. When you specify the fixtures to use in your test place them in order of dependence.Example:
class Asset belongs_to :projectin assets.yml
first_asset: id: 1 project_id: 1 # assumes projects.yml has a project with id: 1in your test file:
fixtures :projects, :assetsnotice project is first. also, im not sure if it should be trusted that fixture load order should be the same as the specified order in the test file. so …
Question: could anyone look at Fixture code for 0.10.1? It seems to be so buggy compare to 0.9.5 I had. The code seems to be out of sync in how to use another code
Question: How do I keep the values true and false from turning into 1 and 0?
According to the YAML specs(http://www.yaml.org/refcard.html) “Yes”, “yes”, “true”, 1 are all counted as 1. Same with false and friends.The solution is quite simple. In your YAML file, quote the string like this:
record: field: "true"And the value should be the string “true” rather than 1.
Question: How to I create fixtures for the relationship between two models that have_and_belongs_to each other?
Question: Since rails 0.14, how can I programmatically enumerate all of the objects in a fixture, without having to name them specifically?
Question: How can I enable the yml or csv fixtures to be interpretted as unicode files? I currently want to insert a chinese character as sample data and although the application works from front to back with this data in mysql, I can’t load it as a part of my tests :( Any help is appreciated.
Question: Is there an easy way to use fixtures outside of rails?
Question: Is there a way to force fixtures to load in a certain order? eg the following won’t work in a rake task unless users is already loaded…
friends1:
user_id1: <%= User.find_by_nickname("alan") %>
user_id2: <%= User.find_by_nickname("bob") %>
Question: Is there a way to reference related records in a way like this:
people.yml
bob:
id: 1
name: Bob the builder
jobs.yml
builder:
id: 1
name: Construction worker/builder
jobs_users.yml
employment_bob:
person_id: bob # or people.bob
job_id: builder # or jobs.builder
.bq Since Rails 2, fixture-engine supports autogenerated, hashed, consistent IDs as well as label references between fixtures (through their associations). —gbence
Question: And to complete this kind of fixturing, is there a way to autoincrement or autogenerate id’s, since they are not of semantic value to my models. (I now use:)
<% i=0 %>
first_item:
id: <%= (i=i+1) %>
Just leave them out. The database will fill them in for you.