Ruby on rails 4 RSpec模型测试:失败

Ruby on rails 4 RSpec模型测试:失败,ruby-on-rails-4,rspec,rspec-rails,rspec3,Ruby On Rails 4,Rspec,Rspec Rails,Rspec3,我正在自学RSpec v3.1.7。我已经用rails安装了rspec,g rspec:install到现有的rails应用程序中-新创建的 我创建了一个模型:railsgrspec:modelzombie。进行了迁移,一切顺利 在:app/models/zombie.rb中: class Zombie < ActiveRecord::Base validates :name, presence: true end 在终端中,当我在应用程序中运行dir:rspec spec/m

我正在自学RSpec v3.1.7。我已经用rails安装了rspec,g rspec:install到现有的rails应用程序中-新创建的

我创建了一个模型:railsgrspec:modelzombie。进行了迁移,一切顺利

在:app/models/zombie.rb中:

class Zombie < ActiveRecord::Base
   validates :name, presence: true
end  
在终端中,当我在应用程序中运行dir:rspec spec/models时,我得到:

我在看视频教程,我用RSpec进行了视频测试,一直到后者。我喜欢在第二章减肥。我错过什么了吗?视频是否使用旧版本的rspec作为视频教程

在我的迁移文件中:

class CreateZombies < ActiveRecord::Migration
  def change
     create_table :zombies do |t|

      t.timestamps
     end
  end
end

您的模型不知道名称,因为您没有在迁移中定义属性:

class CreateZombies < ActiveRecord::Migration
  def change
     create_table :zombies do |t|
      t.string :name
      t.timestamps
     end
  end
end
那么这应该很好:

z = Zombie.new(name: 'foo')
z.name
 => 'foo'

我认为您缺少name属性。以下迁移文件将向僵尸模型添加名称属性:

class AddNameToZombies < ActiveRecord::Migration
  def change
    add_column :zombies, :name, :string
  end
end

就是这样

我创建了一个attr_访问器:name,它可以工作是的。但是视频没有属性访问器!我太迷路了!检查这两个链接:adn Nothing工作正常。在问问题之前,我确实尝试过添加表名,但事情是一样的。我们会更加努力,因为这就是问题所在
rake db:migrate
z = Zombie.new(name: 'foo')
z.name
 => 'foo'
class AddNameToZombies < ActiveRecord::Migration
  def change
    add_column :zombies, :name, :string
  end
end
rake db:migrate

rake db:test:prepare