0

在使用 Rails 3.2 时遇到的这个问题,我一直在努力解决问题。我有一个简单的集成测试可以做到这一点。

require 'test_helper'

class UserFlowsTest < ActionDispatch::IntegrationTest
  fixtures :posts, :comments

  test "should delete comment" do
    post = posts(:one)
    comment = comments(:one)
    delete post_comment_path, :post_id => post.id, :id => comment.id
    assert_response :redirect
    assert_assign (:post)
  end
end

您可以从 Rails 指南中识别模型。无论如何,当我打电话时,rake:integration我最终得到了这个。

test_should_delete_comment(UserFlowsTest):
ActionController::RoutingError: No route matches {:action=>"show", :controller=>"comments"}
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.12/lib/action_dispatch/routing/route_set.rb:533:in `raise_routing_error'
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.12/lib/action_dispatch/routing/route_set.rb:529:in `rescue in generate'
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.12/lib/action_dispatch/routing/route_set.rb:521:in `generate'
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.12/lib/action_dispatch/routing/route_set.rb:562:in `generate'
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.12/lib/action_dispatch/routing/route_set.rb:587:in `url_for'
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.12/lib/action_dispatch/routing/url_for.rb:148:in `url_for'
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.12/lib/action_dispatch/routing/route_set.rb:213:in `post_comment_path'
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.12/lib/action_dispatch/testing/integration.rb:382:in `method_missing'
    C:/dev/blog/test/integration/user_flows_test.rb:9:in `block in <class:UserFlowsTest>'

我的路线是这样定义的。

             root        /                                           home#index
     posts_search GET    /posts/search(.:format)                     posts#search
       home_index GET    /home/index(.:format)                       home#index
    post_comments GET    /posts/:post_id/comments(.:format)          comments#index
                  POST   /posts/:post_id/comments(.:format)          comments#create
 new_post_comment GET    /posts/:post_id/comments/new(.:format)      comments#new
edit_post_comment GET    /posts/:post_id/comments/:id/edit(.:format) comments#edit
     post_comment GET    /posts/:post_id/comments/:id(.:format)      comments#show
                  PUT    /posts/:post_id/comments/:id(.:format)      comments#update
                  DELETE /posts/:post_id/comments/:id(.:format)      comments#destroy
            posts GET    /posts(.:format)                            posts#index
                  POST   /posts(.:format)                            posts#create
         new_post GET    /posts/new(.:format)                        posts#new
        edit_post GET    /posts/:id/edit(.:format)                   posts#edit
             post GET    /posts/:id(.:format)                        posts#show
                  PUT    /posts/:id(.:format)                        posts#update
                  DELETE /posts/:id(.:format)                        posts#destroy

我究竟做错了什么?

编辑:

class CommentsController < ApplicationController
  def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.create(params[:comment])
    redirect_to post_path(@post)
  end

  def destroy
    @post = Post.find(params[:post_id])
    @comment = @post.comments.find(params[:id])
    @comment.destroy
    redirect_to post_path(@post)
  end
end

不知道这里可能出了什么问题。

4

1 回答 1

1

我已经有一段时间没有定期使用 Test::Unit(这些天使用 RSpec),但我不记得有太多需要集成测试。大部分测试是单元测试和功能测试。

这个特定的测试并不是真正的集成测试。它不是测试跨域的信息流。它只是确保在销毁嵌套资源后有重定向。这是我要尝试的。请原谅任何过时的语法。假设一个文件test/functional/comments_controller_test.rb

require 'test_helper'

class CommentsControllerTest < ActionController::TestCase
  fixtures :posts, :comments

  def test_destroy
    post = posts(:one)
    comment = comments(:one)
    delete :destroy, :id => comment.id, :post_id => post.id

    assert_redirected_to post_path(post)
    # and whatever else you want to test
  end
end
于 2013-04-21T15:30:19.303 回答