2015-09-11 63 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

相關問題