更新 - 典型的用户错误 - 伙计感谢所有技术帮助!看起来应该从未制作过password_reset_controller_test。出于某种原因 - 即使有一个" - no-test-framework"用于12.1.1。我很确定我错误输入了某些东西&这就是为什么教程没有为用户提供ID的原因。回顾一下:解决方案是删除控制器测试 - 因为之后进行了集成测试。 / p>
我遇到了一个声明没有路由的错误的问题,但是可以清楚地看到routes.rb文件中的路由&在耙路线。我正在使用Michael Hartl的Ruby on Rails教程指南。在第12章中,我得到了一个失败的测试。我通过删除和添加几个部分来测试这个 - 它实际上似乎在控制器中,即使它声称没有找到任何动作。我也知道如何利用我有限的知识来检查params问题。
任何提示都将不胜感激!
谢谢...
ERROR["test_should_get_edit", PasswordResetsControllerTest, 2016-10-20 15:24:37 +0000]
test_should_get_edit#PasswordResetsControllerTest (1476977077.08s)
ActionController::UrlGenerationError: ActionController::UrlGenerationError: No route matches {:action=>"edit", :controller=>"password_resets"}
test/controllers/password_resets_controller_test.rb:11:in `block in <class:PasswordResetsControllerTest>'
test/controllers/password_resets_controller_test.rb:11:in `block in <class:PasswordResetsControllerTest>'
54/54: [=======================================================================] 100% Time: 00:00:03, Time: 00:00:03
Finished in 3.02339s
54 tests, 279 assertions, 0 failures, 1 errors, 0 skips
控制器测试
password_resets_controller_test
require 'test_helper'
class PasswordResetsControllerTest < ActionController::TestCase
test "should get new" do
get :new
assert_response :success
end
test "should get edit" do
get :edit
assert_response :success
end
end
的routes.rb
Rails.application.routes.draw do
root 'static_pages#home'
get '/help', to: 'static_pages#help'
get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
get '/signup', to: 'users#new'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
resources :users
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :microposts, only: [:create, :destroy]
end
控制器文件
class PasswordResetsController < ApplicationController
before_action :get_user, only: [:edit, :update]
before_action :valid_user, only: [:edit, :update]
before_action :check_expiration, only: [:edit, :update] # Case (1)
def new
end
def create
@user = User.find_by(email: params[:password_reset][:email].downcase)
if @user
@user.create_reset_digest
@user.send_password_reset_email
flash[:info] = "Email sent with password reset instructions"
redirect_to root_url
else
flash.now[:danger] = "Email address not found"
render 'new'
end
end
def edit
end
def update
if params[:user][:password].empty?
@user.errors.add(:password, "can't be empty")
render 'edit'
elsif @user.update_attributes(user_params)
log_in @user
@user.update_attribute(:reset_digest, nil)
flash[:success] = "Password has been reset."
redirect_to @user
else
render 'edit'
end
end
private
def user_params
params.require(:user).permit(:password, :password_confirmation)
end
# Before filters
def get_user
@user = User.find_by(email: params[:email])
end
# Confirms a valid user.
def valid_user
unless (@user && @user.activated? &&
@user.authenticated?(:reset, params[:id]))
redirect_to root_url
end
end
# Checks expiration of reset token.
def check_expiration
if @user.password_reset_expired?
flash[:danger] = "Password reset has expired."
redirect_to new_password_reset_url
end
end
end
这里有rake路由文件 - 如果我在那里读到这里有一个编辑选项...当我删除资源时编辑路由文件会得到不同的错误信息。
rake routes
Prefix Verb URI Pattern Controller#Action
root GET / static_pages#home
help GET /help(.:format) static_pages#help
about GET /about(.:format) static_pages#about
contact GET /contact(.:format) static_pages#contact
signup GET /signup(.:format) users#new
login GET /login(.:format) sessions#new
POST /login(.:format) sessions#create
logout DELETE /logout(.:format) sessions#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
edit_account_activation GET /account_activations/:id/edit(.:format) account_activations#edit
password_resets POST /password_resets(.:format) password_resets#create
new_password_reset GET /password_resets/new(.:format) password_resets#new
edit_password_reset GET /password_resets/:id/edit(.:format) password_resets#edit
password_reset PATCH /password_resets/:id(.:format) password_resets#update
PUT /password_resets/:id(.:format) password_resets#update
microposts POST /microposts(.:format) microposts#create
micropost DELETE /microposts/:id(.:format) microposts#destroy
答案 0 :(得分:1)
而不是get :edit
,请尝试get '/password_resets/new'
要检查路径路径,请在终端中运行rake routes
并查看
:edit
和:new
操作的等效路径。
答案 1 :(得分:1)
我认为你应该通过请求传递必需的参数,如下所示:
test "should get edit" do
get :edit, { id: <id-of-the-resource-for-your-controller> }
assert_response :success
end