我在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
不确定这里有什么不妥。
答案 0 :(得分: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