如何在具有最大的控制器上测试多态性关联



我很难为我的帖子控制器进行测试通行证。通过多态关联在帖子控制器内测试我的创建动作的正确方法是什么?

我正在使用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>'

从我的理解中,我相信这是告诉我,它找不到创建动作是在帖子还是主题上发布的。该网站在开发和生产方面非常有效。问题在此测试中。我如何重写此测试并进行测试,以便认识到它发布给谁?

我不久后找到了一个解决方案。

在这种情况下,要成功测试是否在主题上创建帖子,请确保提供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

最新更新