【发布时间】:2021-07-13 11:35:44
【问题描述】:
我正在关注 Michael Hart 的 The Ruby on Rails。在第 10 章中,我在运行单元测试时遇到了意外错误。 这是我的错误:
ERROR UsersControllerTest#test_should_redirect_edit_when_not_logged_in (1.01s) Minitest::UnexpectedError: ActionController::UrlGenerationError: 没有路由匹配 {:action=>"edit", :controller=>"users"},缺少必需的键:[:id] test/controllers/users_controller_test.rb:16:in `block in class:UsersControllerTest'
这是我的路线:
Rails.application.routes.draw do
scope "(:locale)", locale: /#{I18n.available_locales.join("|")}/ 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
end
end
这是我在 user_contrller_test 中的代码
require "test_helper"
class UsersControllerTest < ActionDispatch::IntegrationTest
def setup
@user = users(:michael)
@other_user = users(:archer)
end
test "should get new" do
get signup_path
assert_response :success
end
test "should redirect edit when not logged in" do
get edit_user_path(@user)
assert_not flash.empty?
assert_redirected_to login_url
end
end
我在固定装置中定义了迈克尔。我尝试使用像get :edit, user_id: @user.id 这样的路径,但它仍然是一个错误。我已经运行了服务器并访问了诸如“/user/765/edit”之类的 URL,它已成功重定向到登录 URL。
我该如何解决这些问题。
【问题讨论】:
-
我认为
@user.id是零 -
我已经调试过了,@user.id 还是有值的,比如“762146111”。
标签: ruby-on-rails unit-testing