安装新的Rails版本后,测试失败

时间:2014-02-15 01:20:25

标签: ruby-on-rails unit-testing ruby-on-rails-4

从4.0.0安装Rails 4.0.2后,我的测试失败了。它适用于浏览器(开发)。 Rails 4.0.3也会出现这种情况。

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 "/users/login", username: users(:chloe).username, password: 'passpasspass'

这是line _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 "/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 "/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)
=> "/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]

3 个答案:

答案 0 :(得分:2)

也许您的用户没有ID:

post.user.id => id

这意味着后期用户不会被保留?

答案 1 :(得分:0)

我必须在后置灯具中将用户ID从0更改为1。那套连锁反应的变化。我还将if放在HAML中的另一行而不是行的末尾,以防万一。 HAML属性似乎在if语句之前进行了评估。

答案 2 :(得分:0)

您的测试正在测试一个收集路由,:index,但测试显示成员路由上的错误,正如它所查找的那样:id。

您要实例化部分(app / views / posts / _post.haml),其中包含带有nil id的link_to,或者您的路径文件是问题。