2014-02-15 58 views
-2

從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://api.rubyonrails.org/classes/ActionDispatch/Integration/RequestHelpers.html#method-i-post_via_redirect

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] 
+0

你可以發佈你的「/ users/login /」控制器的顯着動作嗎?它可能是一個強大的參數問題?最簡單的診斷方法是在重定向之前的位置進行binding.pry。 – plasticide

+0

這與登錄無關,因爲它是重定向的。顯示帖子後,這似乎是失敗的,這是登錄後。這就是它:'session [:user_id] = @ user.id; redirect_to root_url,:notice =>「登錄!從#{lastIP}上次看到。」「。這不僅僅是一個,幾十個測試都失敗了。 – Chloe

+0

對不起 - 我編輯了我原來的評論。 – plasticide

回答

0

我不得不在後置裝置中將用戶標識從0更改爲1。那套連鎖反應的變化。爲了以防萬一,我還將if放在HAML的不同行上,而不是放在行尾。 HAML屬性似乎在if語句之前被評估。

2

也許你的用戶沒有ID:

post.user.id => id 

這將米ean post用戶不會持久?

+0

更改版本前通過了所有測試。用戶在燈具中有一個ID。 – Chloe

0

您的測試正在測試集合路徑:index,但測試在成員路由上顯示錯誤,正如它尋找一個id所示。

你要麼實例化你的部分(app/views/posts/_post.haml),其中有一個link_to與一個零ID或你的路線文件是問題。

+0

不,它不是路由文件。 'user_path(post.user)'是一個成員路由。這是因爲在版本4.0.0中,Rails允許一個關係id不存在的fixture,但是在版本4.0.3中,如果關係id不存在,它會引發異常。 Post.user_id在燈具中設置爲0,用戶ID 0在燈具中不存在。用戶ID從1開始。因此它死了。 – Chloe

相關問題