從4.0.0安裝Rails 4.0.2後,我的測試失敗。它在瀏覽器(開發)中工作。這也發生在Rails 4.0.3中。安裝新的Rails版本後,測試失敗
20) Error:
UserFlowsTest#test_login_and_browse_site:
ActionView::Template::Error: No route matches {:id=>nil} missing required keys: [:id]
app/views/posts/_post.haml:10:in `_app_views_posts__post_haml__439576154_44475312'
app/views/posts/index.haml:1:in `_app_views_posts_index_haml__194952016_42560292'
app/controllers/posts_controller.rb:22:in `block (2 levels) in index'
app/controllers/posts_controller.rb:21:in `index'
test/integration/user_flows_test.rb:14:in `block in <class:UserFlowsTest>'
下面是一行user_flows_test:14
post_via_redirect "https://stackoverflow.com/users/login", username: users(:chloe).username, password: 'passpasspass'
下面是一行_post.haml:10
%a{href: user_path(post.user)}= post.user.username if post.user
http://guides.rubyonrails.org/testing.html#helpers-available-for-integration-tests
測試輸出:
-----------------------------------------
UserFlowsTest: test_login_and_browse_site
-----------------------------------------
Started GET "https://stackoverflow.com/users/login" for 127.0.0.1 at 2014-02-14 21:35:13 -0500
Processing by UsersController#login as HTML
Rendered users/_login.haml (0.0ms)
Rendered users/_new.haml (41.0ms)
Rendered layouts/_header.haml (0.0ms)
Rendered layouts/_right_bar.haml (1.0ms)
Completed 200 OK in 47ms (Views: 47.0ms | ActiveRecord: 0.0ms)
Started POST "https://stackoverflow.com/users/login" for 127.0.0.1 at 2014-02-14 21:35:13 -0500
Processing by UsersController#loginCreate as HTML
Parameters: {"username"=>"chloe", "password"=>"[FILTERED]"}
"*********************************************************************** 1"
Redirected to https://www.example.com/
Completed 302 Found in 160ms (ActiveRecord: 0.0ms)
Started GET "/" for 127.0.0.1 at 2014-02-14 21:35:13 -0500
Processing by PostsController#index as HTML
Rendered posts/_vote.haml (7.0ms)
Rendered posts/_post.haml (18.0ms)
Completed 500 Internal Server Error in 45ms
E-------------------------------
這是UsersController#結束loginCreate
session[:user_id] = @user.id
p "*********************************************************************** #{session[:user_id]}"
redirect_to root_url, :notice => "Logged in! Last seen from #{lastIP}."
這是Rails的控制檯
irb(main):009:0> p = Post.first
irb(main):011:0> app.user_path(p.user)
=> "https://stackoverflow.com/users/1"
這是最簡單的錯誤
21) Error:
PostsControllerTest#test_should_get_index:
ActionView::Template::Error: No route matches {:id=>nil} missing required keys: [:id]
app/views/posts/_post.haml:10:in `_app_views_posts__post_haml__1028637779_43998912'
app/views/posts/index.haml:1:in `_app_views_posts_index_haml__48584214_43731948'
app/controllers/posts_controller.rb:22:in `block (2 levels) in index'
app/controllers/posts_controller.rb:21:in `index'
test/controllers/posts_controller_test.rb:26:in `block in <class:PostsControllerTest>'
這麼簡單......
test "should get index" do
get :index ###### LINE 26
assert_response :success
assert_not_nil assigns(:posts)
end
更多信息
$ RAILS_ENV=test rails console
DL is deprecated, please use Fiddle
Loading test environment (Rails 4.0.3)
irb(main):001:0> Post.pluck(:user_id)
=> [0, 1, 2]
你可以發佈你的「/ users/login /」控制器的顯着動作嗎?它可能是一個強大的參數問題?最簡單的診斷方法是在重定向之前的位置進行binding.pry。 – plasticide
這與登錄無關,因爲它是重定向的。顯示帖子後,這似乎是失敗的,這是登錄後。這就是它:'session [:user_id] = @ user.id; redirect_to root_url,:notice =>「登錄!從#{lastIP}上次看到。」「。這不僅僅是一個,幾十個測試都失敗了。 – Chloe
對不起 - 我編輯了我原來的評論。 – plasticide