title: Unit testing a polymorphic association with fixtures
body: <p>I'm getting this error message:</p>
  
  <pre><code>ERROR["test_profile_display", UsersProfileTest, 2.569931] test_profile_display#UsersProfileTest (2.57s)
  NoMethodError:         NoMethodError: undefined method `posts' for nil:NilClass
          app/controllers/users_controller.rb:14:in `show'
          test/integration/users_profile_test.rb:22:in `block in &lt;class:UsersProfileTest&gt;'
      app/controllers/users_controller.rb:14:in `show'
      test/integration/users_profile_test.rb:22:in `block in &lt;class:UsersProfileTest&gt;'
  </code></pre>
  
  <p>It seems <code>character</code> in the UsersController in the line <code>@posts = @user.character.posts.paginate(page: params[:page])</code> is nil. There is a polymorphic relationship between users and characters. I thought I'd used the correct syntax in fixtures to tell rails that the user Bazley has a fixture:
  In characters.yml:</p>
  
  <pre><code>character_bazley:
    sociable: bazley (user)
  </code></pre>
  
  <p>How do I convince rails that the user fixture does indeed have a character belonging to it?</p>
  
  <p>test/integration/users_profile_test.rb:</p>
  
  <pre><code>require 'test_helper'
  
  class UsersProfileTest &lt; ActionDispatch::IntegrationTest
    include ApplicationHelper
  
    def setup
      @user = users(:bazley)
    end
  
    test "profile display" do
      log_in_as(@user)
      get user_path(@user)
      assert_template 'users/show'
      assert_select 'title', full_title(@user.name)
      assert_select 'h1', text: @user.name
      assert_select 'h1&gt;img.gravatar'
      assert_match @user.character.posts.count.to_s, response.body
      assert_select 'div.pagination'
      @user.character.posts.paginate(page: 1).each do |post|
        assert_match post.content, response.body
      end
    end
  end # UsersProfileTest
  </code></pre>
  
  <p>user.rb:</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
  
    attr_accessor :remember_token, :activation_token, :reset_token
    has_one  :character, as: :sociable, dependent: :destroy
    .
    .
  end
  </code></pre>
  
  <p>character.rb:</p>
  
  <pre><code>class Character &lt; ActiveRecord::Base
  
    belongs_to :sociable, polymorphic: true
    has_many   :posts, dependent: :destroy
    validates  :sociable_id, presence: true
  end # Character
  </code></pre>
  
  <p>users.yml:</p>
  
  <pre><code>bazley:
    name: Bazley
    email: bazley@oxdorf.com
    callsign: bazley
    password_digest: &lt;%= User.digest('password') %&gt;
    admin: true
    activated: true
    activated_at: &lt;%= Time.zone.now %&gt;
  </code></pre>
  
  <p>characters.yml:</p>
  
  <pre><code>character_bazley:
    sociable: bazley (user)
  </code></pre>
  
  <p>In UsersController:</p>
  
  <pre><code>def show
    @user = User.find_by_callsign(params[:callsign])
    @posts = @user.character.posts.paginate(page: params[:page]) # the line causing the error
    @page_name = "user_page"
    redirect_to root_url and return unless @user.activated
  end
  </code></pre>