ActionController :: UrlGenerationError:没有路由匹配{:action =>"编辑",:controller =>"帖子"} - rake测试失败,但app工作正常

时间:2015-03-20 02:16:16

标签: ruby-on-rails ruby ruby-on-rails-3

当我运行rake测试时,会出现两个错误。但是,当我运行我的博客时,索引,显示,新建和编辑视图都能正常工作,并且不会在浏览器中生成错误。我的测试如何失败,但我的博客没有?我已经查看了有关此主题的其他答案,但大多数情况似乎与方法中的链接或嵌套元素有关。我没有。

1) Error:
PostsControllerTest#test_should_get_edit:
ActionController::UrlGenerationError: No route matches {:action=>"edit", :controller=>"posts"}
test/controllers/posts_controller_test.rb:25:in `block in <class:PostsControllerTest>'


2) Error:
PostsControllerTest#test_should_get_show:
ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=>"posts"}
test/controllers/posts_controller_test.rb:15:in `block in <class:PostsControllerTest>'

这是我的show.html.erb

<h1><%= @post.title %></h1>
<h3> Posted: <%= @post.created_at.strftime("%I:%M %p") %> by Tarrence</h3>
<p><%= @post.body %></p>

</div>
<%= render 'sidebar' %>

这是我的edit.html.erb

<h1>Edit</h1>
<%= render 'form' %>
</div>

这是我的_form.html.erb

<%= form_for(@post) do |f| %>
  <%= f.label :title %>
  <%= f.text_field :title, class: "form-control" %>

  <%= f.label :topic %>
  <%= f.text_field :topic, class: "form-control" %>

  <%= f.label :body %>
  <%= f.text_area :body, class: "form-control" %>

  <%= f.submit "Post", class: "btn btn-primary submit" %>
<% end %>

这是我的index.html.erb

<div class="blog-post">
        <% @posts.each do |post| %>
            <h3><%= post.title %></h3>
            <span>Posted by Tarrence <%= post.created_at.strftime("at %I:%M %p") %></span><br>
            <span>Topic: <%= post.topic %></span>

            <% if post.body.length > 100 %>
                <p><%= truncate(post.body, length: 100, separator: ' ') %>
                <%= link_to "Read More", post_path(post) %></p>
            <% else %>
                <p><%= post.body %></p>
            <% end %>

            <hr>
        <% end %>

    </div>

    <nav class="pager">
        <%= will_paginate @posts %>
    </nav>


</div>

这是我的routes.rb

root 'posts#index'
#get 'post' => 'posts#show'
#get 'post/edit' => 'posts#edit'
resources :posts

这是我的posts_controller.rb

class PostsController < ApplicationController
  def index
   # @posts = Post.all
   @posts = Post.paginate(:page => params[:page], :per_page => 2)
  end

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

  def new
    @post = Post.new
  end

  def create
    logger.info "---params ==> #{params.inspect}---"
    @post = Post.new(post_params)
    if @post.save
      redirect_to root_path
    else
      render 'new'
    end
  end

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

  def update
    @post = Post.find(params[:id])
    if @post.update_attributes(post_params)
      redirect_to post_path(@post)
    else
      render 'edit'
    end
  end

  def destroy
  end

  private 

    def post_params
      params.require(:post).permit(:title, :topic, :body)
    end
end

这是我的测试

require 'test_helper'

class PostsControllerTest < ActionController::TestCase

  def setup
    @post = Post.create(title: "This is a post", topic: "Random", body: "This is some text in the body.")
  end

  test "should get index" do
    get :index
    assert_response :success
  end

  test "should get show" do
    get :show, id: @post.id
    assert_response :success
  end

  test "should get new" do
    get :new
    assert_response :success
  end

  test "should get edit" do
    get :edit, id: @post.id
    assert_response :success
  end

  test "should be valid" do
    assert @post.valid?
  end

end

1 个答案:

答案 0 :(得分:7)

您似乎唯一没有表现出错误的地方就是您的测试!

...但这并不重要,因为它清楚问题是什么,你没有在你的测试电话中提供:id,即。你将在test/controllers/posts_controller_test.rb

中做这样的事情
get :show
get :edit

......而你应该做的是这样的事情:

post = Post.create attributes: "required", for: "a post"  # or maybe you use FactoryGirl, you get the idea though - create a post
get :show, id: post.id