【问题标题】:Hartl Chap. 8.2.1 failing test哈特尔章。 8.2.1 未通过测试
【发布时间】:2013-02-12 21:36:51
【问题描述】:

世界上到底发生了什么??我的测试没有通过,我在整个教程中都非常小心(这次)。请告诉我为什么会弹出remember_token错误。

这是我的错误: 失败:

  1) User 
     Failure/Error: it { should respond_to(:remember_token) }
       expected #<User id: nil, name: "Example User", email: "user@example.com",   created_at: nil, updated_at: nil, password_digest:     "$2a$04$HUTzfhPB9eOgNaBIwQZn/.BpGt4h/v9wgLlo.UoMoh8Q..."> to respond to :remember_token
     # ./spec/models/user_spec.rb:28:in `block (2 levels) in <top (required)>'

Finished in 0.19682 seconds
22 examples, 1 failure

Failed examples:

rspec ./spec/models/user_spec.rb:28 # User 

这是我的代码:

db/migrate/[时间戳]_add_remember_token_to_users.rb

class AddRememberTokenToUsers < ActiveRecord::Migration
  def change
    add_column :users, :remember_token, :string
    add_index  :users, :remember_token
  end
end

spec/models/user_spec.rb 需要'spec_helper'

describe User do

  before do
    @user = User.new(name: "Example User", email: "user@example.com", 
                     password: "foobar", password_confirmation: "foobar")
  end

  subject { @user }

  it { should respond_to(:name) }
  it { should respond_to(:email) }
  it { should respond_to(:password_digest) }
  it { should respond_to(:password) }
  it { should respond_to(:password_confirmation) }
  it { should respond_to(:remember_token) }
  it { should respond_to(:authenticate) }

  it { should be_valid }
  it { should respond_to(:authenticate) }
.
.
.  

【问题讨论】:

  • 你记得运行rake db:migrate吗?
  • 是的,我做到了,我做了所有事情......显然没有......
  • 我不确定你的意思......你仍然收到错误吗?

标签: ruby-on-rails-3.2 railstutorial.org


【解决方案1】:

我猜你需要运行rake db:test:prepare。祝你好运!

【讨论】:

    【解决方案2】:

    我遇到了一个非常相似的问题,我们在here 进行了报道。

    总而言之,数据库出了点问题,所以我所做的是删除表,然后重新运行 rake db:migraterake db:test:prepare

    删除表格:

    • 打开 SQLite 数据库浏览器(Hartl 在本书前面推荐的数据库浏览器)。
    • 在 SQLite 数据库浏览器中,打开 sample_app/db/development.sqlite。
    • 删除(从 SQLite 数据库浏览器),删除 schema_migrations 表。
    • 我还必须从终端删除 schema.rb。

    我确实尝试了这些步骤,然后运行了rake db:migrate,但遇到了另一个错误。然后我删除了整个 users 表(在 SQLite DB 浏览器中),然后重新运行 rake db:migrate 并让一切顺利通过。

    在执行此操作之前备份。

    【讨论】:

      【解决方案3】:

      我今天也遇到了这个问题。 我不得不手动删除 [timestamp]_add_remember_token_to_users.rb 文件。 然后运行 bundle exec rake db:migrate bundle exec rake db:test:prepare

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-07
        • 1970-01-01
        • 1970-01-01
        • 2023-03-06
        • 2020-08-08
        相关资源
        最近更新 更多