灯具中的关联不正确

时间:2014-06-01 23:56:54

标签: ruby-on-rails ruby fixtures

我正在使用Rails 4.1.1跟踪指南http://guides.rubyonrails.org/testing.html#the-low-down-on-fixtures,并在https://github.com/codetriage/codetriage项目中使用命名夹具。但是,当我尝试从另一个夹具引用一个夹具时,它不起作用:

# 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

# 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

您可以看到问题应该是加载repo issue_triage_sandbox repo。但在我的测试中,它不是:

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>     

为什么要创建这个问题并提到不存在的回购?

2 个答案:

答案 0 :(得分:1)

我认为必须使用repos夹具中的id属性。 我正在使用yml灯具。我将问题和回购添加到项目中的现有文件中。

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

从控制台:

rake db:fixtures:加载RAILS_ENV = test rails c test

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

=&GT; #https://api.github.com/repos/bemurphy/issue_triage...",repo_name:nil,user_name:nil,last_touched_at:&#34; 2012-11-10 22:20:24&#34 ;,数字:1,created_at:&#34; 2012-11-10 23:23:45&#34;,updated_at:&#34; 2012-11-10 23:23:45&#34;,repo_id:915227508,标题:&#34;首次测试问题在sinatra&#34;,html_url:&#34; https://github.com/sinatra/sinatra/issues/1&#34;,州:&#34;打开&#34;,pr_attached:false&gt;

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

答案 1 :(得分:0)

根据项目here中的代码,Issues似乎属于Repos。我想知道固定装置是否在这种关系中“倒退”了。

您可以尝试使用ERB获取正确夹具的ID并将其应用如下: repo_id: <%= ActiveRecord::FixtureSet.identify(:issue_triage_sandbox) %>