2015-09-11 64 views
1

我将发布问题下面的所有文件。我对测试非常陌生。我正在为我的应用编写一个控制器测试。该模型是所有权。所有权归团队所有。我使用回形针宝石为每个团队附上徽标。Rails 4 - 控制器测试错误,Active Record关联问题

当我运行测试,我得到了如下错误:

1) Error: 
OwnershipsControllerTest#test_Should_retrieve_results: 
ActionView::Template::Error: undefined method `logo_file_name' for nil:NilClass 
app/views/ownerships/round1.html.erb:23:in `block in _app_views_ownerships_round__html_erb___2932533286089946925_70246915278440' 
app/views/ownerships/round1.html.erb:18:in `each' 
app/views/ownerships/round1.html.erb:18:in `_app_views_ownerships_round__html_erb___2932533286089946925_70246915278440' 
test/controllers/ownership_controller_test.rb:12:in `block in <class:OwnershipsControllerTest>' 

它抱怨我如何处理在视图中缺少的标志。这是它不喜欢的round1.html.erb的部分:

<% if ownership.team.logo_file_name == nil %> 
    <td></td> 
    <% else %> 
     <td><%= image_tag ownership.team.logo.url(:medium) %></td> 
    <% end %> 

问题不在于回形针附件本身。任何对Team模型的引用都会引发错误。如果我删除相关的标识代码,下面也产生了同样的错误:

<%= ownership.team.name%>

请告诉我真的傻了眼是我的索引方法测试不抛出一个错误。索引和round1视图的代码是相同的!我如何设置我的测试,以便识别相关模型的属性?

ownership_controller_test.rb

require 'test_helper' 

class OwnershipsControllerTest < ActionController::TestCase 
test "Should retrieve index" do 
get :index 
assert_response :success 
end 

test "Should retrieve results" do 
get :round1 
assert_response :success 
end 
end 

ownerships.yml

one: 
round: 1 
pick: 1 
team_id: 1 
player_id: 1 

two: 
round: 1 
pick: 1 
team_id: 1 
player_id: 1 

teams.yml

one: 
name: Browns 
division: East 
logo_file_name: logo.jpg 

two: 
name: Chargers 
division: West 
logo_file_name: logo.jpg 

模型

class Ownership < ActiveRecord::Base 
belongs_to :player 
belongs_to :team 

validates :round, :pick, :team_id, presence: true 
end 

class Team < ActiveRecord::Base 
has_many :ownerships 
has_many :players, through: :ownerships 

validates :name, presence: true 
validates :division, presence: true 

has_attached_file :logo , :styles => { :small => '10>', :medium => '40>', :large => '60>' } 
validates_attachment_content_type :logo, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"] 
end 

的ownerships_controller.rb

class OwnershipsController < ApplicationController 

def index 
@ownerships = Ownership.all.select { |t| t.player_id == nil} 
Rails.application.config.counter = (256 - Ownership.all.select{ |t| t.player_id == nil}.count) 
end 

def round1 
@ownerships = Ownership.all.select { |m| m.round == 1} 
end 

private 
def ownership_params 
params.require(:ownership).permit(:round,:pick,:team_id,:player_id) 
end 
+0

这是因为如果“所有权”没有“Team”,那就是错误所说的。它说我不知道​​如何在'nil'上调用'logo_file_name',因为'ownership.team#=>无' – engineersmnky

回答

1

相关部分需要添加引用到球队的所有权夹具。你可以通过标签来引用。

one: 
    round: 1 
    pick: 1 
    team: one 
    player_id: 1 

或者,你目前的做法也将工作,只是主键添加到团队

所有权

one: 
    round: 1 
    pick: 1 
    team_id: 1 
    player_id: 1 

one: 
    id: 1 
    name: Browns 
    division: East 
    logo_file_name: logo.jpg 

参见:More info about relations in fixtures

+0

谢谢Swards。我终于明白了。我已经阅读过文档,但他们的例子并不是最好的。 – SeattleDucati

相关问题