【问题标题】:Rails 4 fixtures aren't generating IDs (Hartl tutorial 9.6.2)Rails 4 固定装置不生成 ID(Hartl 教程 9.6.2)
【发布时间】:2015-07-10 15:00:59
【问题描述】:

我正在根据 Hartl 教程创建一个应用程序,但我被困在第二个练习中:

为所有布局链接编写集成测试,包括登录和未登录用户的正确行为。提示:使用 log_in_as 助手添加到清单 5.25 中的测试。

我的测试代码是

require 'test_helper'

class SiteLayoutTest < ActionDispatch::IntegrationTest

def setup
   @user = users(:test)
end

test 'layout links' do
   get root_path
   assert_template 'static_pages/home'
   assert_select "a[href=?]", root_path, count: 2
   assert_select "a[href=?]", help_path
   assert_select "a[href=?]", about_path
   assert_select "a[href=?]", contact_path
   assert_select "a[href=?]", login_path
   get signup_path
   assert_select "title", full_title("Signup")
   log_in_as @user
   get root_path
   assert_select "a[href=?]", users_path, text: "Users"
   assert_select "a[href=?]", user_path
   assert_select "a[href=?]", edit_user_path(@user)
   assert_select "a[href=?]", logout_path

结束 结束

这是我正在使用的夹具:

test:
 name: Test Name
 derby_name: Test Derby Name
 email: test@rdsg.com
 password_digest: <%= User.digest('password') %>
 admin: true

这是我在运行测试时不断遇到的错误:

ERROR["test_layout_links", SiteLayoutTest, 2015-07-07 21:08:39 +0000]                                                              
 test_layout_links#SiteLayoutTest (1436303319.87s)                                                                                 
ActionController::UrlGenerationError:         ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=
>"users"} missing required keys: [:id]                                                                                             
            test/integration/site_layout_test.rb:22:in `block in <class:SiteLayoutTest>'                                           
        test/integration/site_layout_test.rb:22:in `block in <class:SiteLayoutTest>' 

在我看来,我的设备由于某种原因没有生成 ID,即使当我手动运行测试时,我可以正常访问站点导航 URL,并且当我运行 rails console --sandbox 并创建一个新用户时它生成一个 ID 的对象。我试过寻找这个问题的答案,但没有运气(我已经被困在这个问题上好几天了!)。

我正在使用带有 Rails 4.2.0 和 Ruby 2.1.5 的 Codio IDE,并使用 Minitest 作为测试套件。

编辑:作为参考,这是我的 log_in_as 助手:

# Logs in a test user
def log_in_as(user, options = {})
    password = options[:password] || 'password'
    remember_me = options[:remember_me] || '1'
    if integration_test?
        post login_path, session: { email: user.email,
            password: password, 
            remember_me: remember_me }
    else
        session[:user_id] = user.id
    end
end

以防万一将来对任何人有所帮助:)

感谢 Baldrick 在下面解决它!

【问题讨论】:

  • 你能把log_in_as方法的代码贴出来
  • 已添加,但 Baldrick 解决了以下问题

标签: ruby-on-rails-4 testing railstutorial.org fixtures minitest


【解决方案1】:

错误消息的重要部分是ActionController::UrlGenerationError: No route matches {:action=&gt;"show", :controller=&gt;"users"} missing required keys: [:id]user_path方法需要一个用户(它是显示给定用户的路径),所以应该是

assert_select "a[href=?]", user_path(@user)

【讨论】:

  • 现已修复。谢谢!以为我以某种方式弄乱了数据库,所以我很高兴它已排序。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多