如何使用Minitest测试控制器上的多态关联?

时间:2016-12-22 03:29:28

标签: ruby-on-rails-4 functional-testing minitest

我无法为我的帖子控制器制作测试通行证。 使用多态关联在Posts控制器中测试我的创建操作的正确方法是什么?

我正在使用Rails 4

这是我的代码

型号:

class Topic < ActiveRecord::Base
  belongs_to :user
  belongs_to :category
  has_many :posts, as: :postable
  has_many :posts, dependent: :destroy
  validates :name, presence: true, length: {maximum: 50}
  validates :description, presence: true, length: {maximum: 80}
  validates :user_id, presence: true
  is_impressionable
end

class Post < ActiveRecord::Base
  belongs_to :user
  belongs_to :topic
  belongs_to :category
  belongs_to :postable, polymorphic: true
  has_many :posts, as: :postable
  validates :comment, presence: true
  validates :user_id, presence: true
  validates :topic_id, presence: true
  validates :category_id, presence: true
  default_scope { order(created_at: :asc) }
end

发布控制器

class PostsController < ApplicationController
  before_action :auth_user
  before_action :set_post, only: [:destroy]
  before_action :correct_user, only: [:destroy]
  before_action :find_postable, only: [:create, :new]

  def new
    @post = @postable.posts.build
  end

  def create
    @post = @postable.posts.build(post_params)
    set_topic_id
    set_category_id
    @post.user_id = current_user.id
    if @post.save
      redirect_to topic_path(@post.topic.id)
    else
      redirect_to request.referer, notice: "Post unsuccessful!"
    end
  end

  def destroy
    @post.destroy
    flash[:success] = 'Post deleted'
    redirect_to request.referer || root_url
  end

  private

  def set_post
    @post = Post.find(params[:id])
  end

  def correct_user
    @post = current_user.posts.find_by(id: params[:id])
    redirect_to root_url if @post.nil?
  end

  def find_topic
    @topic = Topic.find(params[:topic_id])
  end

  def find_postable
    @postable = Post.find_by_id(params[:post_id]) if params[:post_id]
    @postable = Topic.find_by_id(params[:topic_id]) if       
     params[:topic_id]
  end

  def post_params
    params.require(:post).permit(:comment)
  end
end

控制后测试:

require 'test_helper'

class PostsControllerTest < ActionController::TestCase

  def setup
    @topic = topics(:topicone)
    @post = posts(:postone)
    @posttwo = posts(:posttwo)
    @category = categories(:categoryone)
    @user = users(:user1)
  end

  test 'should create post when logged in' do
    sign_in @user
    assert_difference 'Post.count', 1 do
      post :create, post: { user_id: @user.id, category_id:    
      @category.id, topic_id: @topic.id, comment: "First reply!",  
      postable_id: @post.id, postable_type: "Post" }
    end
  end
end

当我运行上面的测试时,我收到此错误:

ERROR["test_should_create_post_when_logged_in", PostsControllerTest,    
  2016-12-04 14:23:25 -0500]
  test_should_create_post_when_logged_in#PostsControllerTest   
  (1480879405.93s)
NoMethodError: NoMethodError: undefined method `posts' for nil:NilClass
        app/controllers/posts_controller.rb:13:in `create'
        test/controllers/posts_controller_test.rb:28:in `block (2   
levels) in <class:PostsControllerTest>'
        test/controllers/posts_controller_test.rb:25:in `block in   
<class:PostsControllerTest>'
    app/controllers/posts_controller.rb:13:in `create'
    test/controllers/posts_controller_test.rb:28:in `block (2 levels) 
in <class:PostsControllerTest>'
    test/controllers/posts_controller_test.rb:25:in `block in 
<class:PostsControllerTest>'

根据我的理解,我相信它告诉我无法找到创建操作是发布到帖子还是主题。 该网站在开发和生产方面非常有用。问题出在这个测试中。 我怎样才能重写这个测试并使其能够识别出发布给谁?

1 个答案:

答案 0 :(得分:0)

我很快就找到了解决方案。

在这种情况下,要成功测试是否在主题上创建了帖子,请确保提供topic_id。

test 'should create post on a topic when logged in' do
  sign_in @user
  assert_difference 'Post.count', 1 do
    post :create, topic_id: @topic, post: {
                user_id: @user.id,
                category_id: @category.id,
                comment: 'First post on a topic!' }
  end
end

现在要测试是否在其他帖子上创建了帖子,请务必提供post_id。

test 'should create post on another post when logged in' do
  sign_in @user
  assert_difference 'Post.count', 1 do
    post :create, post_id: @post, post: {
                user_id: @user.id,
                category_id: @category.id,
                comment: 'First post on a topic!' }
  end
end