Schneems
Schneems

Reputation: 15828

Incorrect association in fixtures

I'm following the guide http://guides.rubyonrails.org/testing.html#the-low-down-on-fixtures with Rails 4.1.1 and using named fixtures within one another in the https://github.com/codetriage/codetriage project. However when I try to reference one fixture from another it doesn't work:

# fixtures/issues.rb

issue_triage_sandbox_issue:
  id: 4
  comment_count:
  url: https://api.github.com/repos/bemurphy/issue_triage_sandbox/issues/1
  last_touched_at: 2012-11-10 22:20:24.000000000 Z
  number: 1
  created_at: 2012-11-10 23:23:45.281189000 Z
  updated_at: 2012-11-10 23:23:45.281189000 Z
  repo: issue_triage_sandbox
  title: first test issue in sinatra
  html_url: https://github.com/sinatra/sinatra/issues/1
  state: open

and

# fixtures/users.rb

issue_triage_sandbox:
  id: 1
  user_name: bemurphy
  name: issue_triage_sandbox
  full_name: bemurphy/issue_triage_sandbox
  language: ruby
  created_at: 2012-11-10 21:50:48.351554000 Z
  updated_at: 2012-11-10 21:50:48.351554000 Z
  issues_count: 1

You can see that the issue should be loading the repo issue_triage_sandbox repo. But in my tests it's not:

issue = issues(:issue_triage_sandbox_issue)
puts issue.repo
# => nil

puts issue.inspect
#<Issue id: 4, comment_count: nil, url: "https://api.github.com/repos/bemurphy/issue_triage...", repo_name: nil, user_name: nil, last_touched_at: "2012-11-10 22:20:24", number: 1, created_at: "2012-11-10 23:23:45", updated_at: "2012-11-10 23:23:45", repo_id: 915227508, title: "first test issue in sinatra", html_url: "https://github.com/sinatra/sinatra/issues/1", state: "open", pr_attached: false>     

Any ideas why the issue is being created with a reference to a non-existant repo?

Upvotes: 3

Views: 298

Answers (2)

eamexicano
eamexicano

Reputation: 36

I think it has to be with the id attribute in the repos fixture. I'm using yml fixtures. I added the issue and the repo to the existing ones in the project.

issues.yml

issue_triage_sandbox:
  user_name: bemurphy
  name: issue_triage_sandbox
  full_name: bemurphy/issue_triage_sandbox
  language: ruby
  created_at: 2012-11-10 21:50:48.351554000 Z
  updated_at: 2012-11-10 21:50:48.351554000 Z
  issues_count: 1

repos.yml

issue_triage_sandbox:
  user_name: bemurphy
  name: issue_triage_sandbox
  full_name: bemurphy/issue_triage_sandbox
  language: ruby
  created_at: 2012-11-10 21:50:48.351554000 Z
  updated_at: 2012-11-10 21:50:48.351554000 Z
  issues_count: 1

From the console:

rake db:fixtures:load RAILS_ENV=test rails c test

irb(main):001:0> i = Issue.last

=> #https://api.github.com/repos/bemurphy/issue_triage...", repo_name: nil, user_name: nil, last_touched_at: "2012-11-10 22:20:24", number: 1, created_at: "2012-11-10 23:23:45", updated_at: "2012-11-10 23:23:45", repo_id: 915227508, title: "first test issue in sinatra", html_url: "https://github.com/sinatra/sinatra/issues/1", state: "open", pr_attached: false>

irb(main):002:0> i.repo

Upvotes: 1

crsven
crsven

Reputation: 186

Based on the code in your project here, it looks like Issues belong to Repos. I'm wondering if the fixtures are having trouble going "backwards" in that relationship.

You could try using ERB to get the ID of the proper fixture and apply it like so: repo_id: <%= ActiveRecord::FixtureSet.identify(:issue_triage_sandbox) %>

Upvotes: 0

Related Questions