Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a Rails unit test, how can I get a User fixture to load its associated Profile?

In the documentation concerning Fixtures (http://api.rubyonrails.org/classes/Fixtures.html) they provide the following example of using label references for associations:

### in pirates.yml
reginald:
  name: Reginald the Pirate
  monkey: george

### in monkeys.yml
george:
  name: George the Monkey
  pirate: reginald

So following their lead, I have a User model that has_one :profile, a Profile model that belongs_to :user, and tried to set up fixtures per their example:

### in users.yml
reginald:
  id: 1
  login: reginald

### in profiles.yml
reginalds_profile:
  id: 1
  name: Reginald the Pirate
  user: reginald

(Note: since my association is one-way, the User fixture doesn't have a "profile: reginalds_profile" association--putting it in causes an error because the SQL table has no profile_id attribute.)

The problem is, in my unit tests everything seems to load correctly, but users(:reginald).profile is always nil. What am I missing?

like image 530
MikeJ Avatar asked Mar 25 '10 19:03

MikeJ


People also ask

What is Minitest in Ruby on Rails?

What is Minitest? Minitest is a testing tool for Ruby that provides a complete suite of testing facilities. It also supports behaviour-driven development, mocking and benchmarking. With the release of Ruby 1.9, it was added to Ruby's standard library, which increased its popularity.

What is test fixture in unit test framework?

In generic xUnit, a test fixture is all the things that must be in place in order to run a test and expect a particular outcome. Frequently fixtures are created by handling setUp() and tearDown() events of the unit testing framework.

What are fixtures in rails?

Fixtures are data that you can feed into your unit testing. They are automatically created whenever rails generates the corresponding tests for your controllers and models. They are only used for your tests and cannot actually be accessed when running the application.

What are the instance variables available in functional testing?

You also have access to three instance variables in your functional tests: @controller – The controller processing the request. @request – The request. @response – The response.


1 Answers

Based on tadman's suggestion I did some more searching and found the answer elsewhere on this site, so I might as well post it.

See post titled Automatic associations in ruby on rails fixtures

Apparently the way Rails finds associated fixtures when you use labels (user: reginald) instead of IDs (user_id: 1) is by hashing the name and assuming the hash is the ID. If you set the ID to something specific, this fails. But if you let Rails automatically assign IDs it uses that hashing scheme. So the documentation for fixture association labels is missing a key tidbit--if you are using labels you must avoid applying your own IDs in the fixtures to be matched. Fixtures not being matched by labels can still have whatever ID scheme you choose.

like image 191
MikeJ Avatar answered Sep 21 '22 07:09

MikeJ