用夹具对多态关联进行单元测试 [英] Unit testing a polymorphic association with fixtures

查看:55
本文介绍了用夹具对多态关联进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误消息:

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>'

@posts = @user.character.posts.paginate(page: params[:page])行的UsersController中,似乎character为零.用户和角色之间存在多态关系.我以为我在灯具中使用了正确的语法来告诉Rails用户Bazley有灯具: 在character.yml中:

It seems character in the UsersController in the line @posts = @user.character.posts.paginate(page: params[:page]) 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:

character_bazley:
  sociable: bazley (user)

我如何说服Rails用户装置确实具有属于它的字符?

How do I convince rails that the user fixture does indeed have a character belonging to it?

test/integration/users_profile_test.rb:

test/integration/users_profile_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

user.rb:

class User < ActiveRecord::Base

  attr_accessor :remember_token, :activation_token, :reset_token
  has_one  :character, as: :sociable, dependent: :destroy
  .
  .
end

character.rb:

character.rb:

class Character < ActiveRecord::Base

  belongs_to :sociable, polymorphic: true
  has_many   :posts, dependent: :destroy
  validates  :sociable_id, presence: true
end # Character

users.yml:

users.yml:

bazley:
  name: Bazley
  email: bazley@oxdorf.com
  callsign: bazley
  password_digest: <%= User.digest('password') %>
  admin: true
  activated: true
  activated_at: <%= Time.zone.now %>

characters.yml:

characters.yml:

character_bazley:
  sociable: bazley (user)

在UsersController中:

In 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

推荐答案

characters.yml更改为

character_bazley:
  sociable: bazley (User)

代替

character_bazley:
  sociable: bazley (user)

请注意,它是User而不是user.此处User使用的关键字将表明sociable_typeUser类.

Note it is User and not user. The keyword used here User will tell that the sociable_type is of User class.

参考:

http://ruby-journal.com/rails/define-fixtures-with-多态关联/ http://api.rubyonrails.org/v3.2.8/classes/ActiveRecord/Fixtures.html#label-Polymorphic + belongs_to

这篇关于用夹具对多态关联进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆