Ruby on rails 单元测试与夹具的多态关联

Ruby on rails 单元测试与夹具的多态关联,ruby-on-rails,fixtures,Ruby On Rails,Fixtures,我收到以下错误消息: 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

我收到以下错误消息:

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 <class:UsersProfileTest>'
    app/controllers/users_controller.rb:14:in `show'
    test/integration/users_profile_test.rb:22:in `block in <class:UsersProfileTest>'
我如何让rails相信用户装置确实有一个属于它的角色

测试/集成/用户\u配置文件\u test.rb:

require 'test_helper'

class UsersProfileTest < 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>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
在UsersController中:

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

字符.yml
更改为

character_bazley:
  sociable: bazley (User)
而不是

character_bazley:
  sociable: bazley (user)
注意它是
User
,而不是
User
。此处使用的关键字
User
将表明
socialable\u类型
属于
User
类别

参考:


哦,天哪,一个大写字母。。。我是个笨蛋。非常感谢。
bazley:
  name: Bazley
  email: bazley@oxdorf.com
  callsign: bazley
  password_digest: <%= User.digest('password') %>
  admin: true
  activated: true
  activated_at: <%= Time.zone.now %>
character_bazley:
  sociable: bazley (user)
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
character_bazley:
  sociable: bazley (User)
character_bazley:
  sociable: bazley (user)