Rails(哈特教程)表用户没有名为

时间:2015-09-02 21:30:38

标签: ruby-on-rails railstutorial.org

我正在完成Mike Hartl神话般的Rails教程(V3)的第9章。一切都顺利进行,直到我尝试为users_index页面设置集成测试。

相关文件,我知道:

users.yml里

michael:
name: Michael Example
email: michael@example.com
password_digest: <%= User.digest('password') %>

archer:
name: Sterling Archer
email: duchess@example.gov
password_digest: <%= User.digest('password') %>

lana:
name: Lana Kane
email: hands@example.gov
password_digest: <%= User.digest('password') %>

mallory:
name: Mallory Archer
email: boss@example.gov
password_digest: <%= User.digest('password') %>

<% 30.times do |n| %>
user_<%= n %>:
name: <%=  "User #{n}" %>
email: <%= "user-#{n}@example.com" %>
password_digest: <%= User.digest('password') %>
<% end %>
编辑:在users.yml中的最后一个块肯定是以某种方式涉及,当我删除它时,其余的测试通过就好......但我认为这是与此代码交互的数据库,而我是&m; m不确定如何调试它。

users_index_test.rb:

require 'test_helper'

class UsersIndexTest < ActionDispatch::IntegrationTest

def setup
@user = users(:michael)
end

test "index including pagination" do
log_in_as(@user)
get users_path
assert_template 'users/index'
assert_select 'div.pagination'
User.paginate(page: 1).each do |user|
  assert_select 'a[href=?]', user_path(user), text: user.name
end
end

end

据我所知,这些与文本和参考实现相匹配,但是当我

bundle exec rake test

对于套件中的所有34个测试,我得到的输出就像这样:

ERROR["test_should_redirect_edit_when_not_logged_in" UsersControllerTest, 2015-09-02 14:05:27 +0000]
test_should_redirect_edit_when_not_logged_in#UsersControllerTest (1441202727.48s)
ActiveRecord::StatementInvalid:         ActiveRecord::StatementInvalid: SQLite3::SQLException: table users has no column named user_0: INSERT INTO "users" ("name", "email", "password_digest", "user_0", "user_1", "user_2", "user_3", "user_4", "user_5", "user_6", "user_7", "user_8", "user_9", "user_10", "user_11", "user_12", "user_13", "user_14", "user_15", "user_16", "user_17", "user_18", "user_19", "user_20", "user_21", "user_22", "user_23", "user_24", "user_25", "user_26", "user_27", "user_28", "user_29", "created_at", "updated_at", "id") VALUES ('User 29', 'user-29@example.com', '$2a$04$WuzW.yzF5SQELTCJdmvA2ubopDH0BA305WKiysdCI1pkzlCPv.v/.', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, '2015-09-02 20:42:20', '2015-09-02 20:42:20', 785162917)

显然有些东西已关闭,但我没有足够的经验来跟踪它。我发现的最接近的问题给出了类似的输出(&#34; SQLite3 :: SQLException:表用户没有列名为......&#34;),但我见过的解决方案通常涉及与tab的yaml间距(我做了一定要在教程之前提醒我之后检查。

1 个答案:

答案 0 :(得分:3)

嗯,原来这是一个YAML间距问题 - 但我不完全理解为什么。

不起作用:

<% 30.times do |n| %>
  user_<%= n %>:
  name: <%= "User #{n}" %>
  email: <%= "user-#{n}@example.com" %>
  password_digest: <%= User.digest('password') %>
<% end %>

不起作用:

<% 30.times do |n| %>
user_<%= n %>:
name:  <%= "User #{n}" %>
email: <%= "user-#{n}@example.com" %>
password_digest: <%= User.digest('password') %>
<% end %>

工作:

<% 30.times do |n| %>
user_<%= n %>:
  name:  <%= "User #{n}" %>
  email: <%= "user-#{n}@example.com" %>
  password_digest: <%= User.digest('password') %>
<% end %>

如果第2行是缩进的,或者如果第3,4或5行没有缩进,它会在我的测试套件中中断所有34个测试和116个断言。