I will post all files below the question. I am very new to testing. I'm trying to write a controller test for my app. The model is Ownerships. Ownerships is owned by Team. I'm using the paperclip gem to attach a logo to each team.
When I run the test, I get the follow error:
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>'
It's complaining about how I'm dealing with a missing logo in the view. This is the portion of the round1.html.erb it doesn't like:
<% if ownership.team.logo_file_name == nil %>
<td></td>
<% else %>
<td><%= image_tag ownership.team.logo.url(:medium) %></td>
<% end %>
The problem is not the paperclip attachment itself. ANY reference to the Team model throws an error. If I remove the code related to the logo, the following also generates the same error:
<%= ownership.team.name %>
Whats really perplexing is the my test for the index method doesn't throw an error. The code for the index and round1 views are the same! How can I setup my test so that the attributes from the associated models are recognized?
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
Models
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
The relevant parts of 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
You need to add references to team in your ownership fixture. You can reference by label.
one:
round: 1
pick: 1
team: one
player_id: 1
Or, your current approach would also work, just add the primary key to the team
Ownership
one:
round: 1
pick: 1
team_id: 1
player_id: 1
Team
one:
id: 1
name: Browns
division: East
logo_file_name: logo.jpg
See also: More info about relations in fixtures
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.