MiniTest - 未找到路线

时间:2015-05-21 18:14:20

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

我有一个POST模型,控制器和视图,我正在创建它的测试套件,以确保所有内容都经过正确测试。我现在正在创建集成测试,并且似乎总是得到相同的错误,表示在运行“编辑成功发布”测试时ID丢失了。

我真的不明白我做错了什么。错误消息表明缺少一个:id键,但是在创建新帖子时不应该自动创建一个?我做错了什么?

版本:
Ruby:2.1.2
Rails:4.2.1

整合测试

require 'test_helper'

class PostsCrudTest < ActionDispatch::IntegrationTest
  def setup
    @post = { title: "This is the title",
               content: "Detailed comment."*10,
               phone: 9991118888,
               email: "email@h_list.com",
               user_id: users(:spiderman).id }
  end

  test "creates a new post successfully" do 
    get new_post_path
    assert_template 'posts/new'
    assert_difference 'Post.count', 1 do 
      post posts_path, post: @post
    end
    assert_template 'posts/show'
  end

  test "fails to create a new post with inaccurate info" do 
    get new_post_path
    assert_no_difference 'Post.count' do 
      post posts_path, post: { title: "", content: "", phone: 1111, email: 00 }
    end
    assert_template 'posts/new'
  end

  test "shows a new post correctly" do 
    get new_post_path
    post posts_path, post: @post
    assert_template 'posts/show'
    assert_equal 'Your new posting was created.', flash[:success]
    assert_select 'h2', @post[:title]
    assert_select 'h2+p', @post[:content]
  end

  test "edits a post successfully" do 
    get new_post_path
    post posts_url, post: @post
    assert_template 'posts/show'

    get edit_post_path, post: @post
    # assert_select '.edit_post' do 
      assert_select 'textarea', @post[:content]
    # end
  end


end

测试结果

$ rake test:integration
Run options: --seed 35902

# Running:

...E

Finished in 0.310827s, 12.8689 runs/s, 32.1722 assertions/s.

  1) Error:
PostsCrudTest#test_edits_a_post_successfully:
ActionController::UrlGenerationError: No route matches {:action=>"edit", :controller=>"posts"} missing required keys: [:id]
test/integration/posts_crud_test.rb:44:in `block in <class:PostsCrudTest>'

4 runs, 10 assertions, 0 failures, 1 errors, 0 skips

相关路线:

               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
                     PATCH  /posts/:id(.:format)           posts#update
                     PUT    /posts/:id(.:format)           posts#update
                     DELETE /posts/:id(.:format)           posts#destroy

注意:一切似乎都在实际网站上运行良好。只是测试套件给了我这个错误。

1 个答案:

答案 0 :(得分:2)

  

错误消息显示a:id键丢失

是的,因为你正在做两个步骤:

  1. 您可以使用@post哈希创建新帖子。我假设帖子是在服务器上创建的,这通常意味着帖子是在数据库中创建的,并且会创建一个新的ID。

  2. 您尝试编辑@post哈希,它没有任何ID。这就是您收到错误消息的原因。

  3. 有很多选择可以解决这个问题。

    选项1很简单:直接在代码中创建帖子,而不是使用new_post_path。

    示例:

    test "edits a post successfully" do 
      p = Post.create(@post)  # or whatever options you want
      get edit_post_path, post: p.id
      ...
    

    选项2是高级的:按照您的方式创建帖子,并使您的服务器返回新创建的帖子的ID。如果需要,可以将其放在HTTP响应中,或者将其放在更加结构化的内容中,例如JSON响应。