我在自学RSpec (v3.1.7)。我已经在一个现有的rails应用中安装了带有rails g rspec:install的rspec --这是我刚刚创建的。
我创建了一个模型:rails g rspec:model zombie。运行迁移,一切都进行得很顺利。
在: app/models/zombie.rb:
class Zombie < ActiveRecord::Base
validates :name, presence: true
end 在:app/spec/model/zombie_spec.rb中:
require 'rails_helper'
RSpec.describe Zombie, :type => :model do
it 'is invalid without a name' do
zombie = Zombie.new
zombie.should_not be_valid
end
end在终端中我运行时(在应用程序目录中):rspec spec/models I得到:
F
Failures:
1) Zombie is invalid without a name
Failure/Error: zombie.should_not be_valid
NoMethodError:
undefined method `name' for #<Zombie id: nil, created_at: nil, updated_at: nil>
# ./spec/models/zombie_spec.rb:6:in `block (2 levels) in <top (required)>'我正在看一个视频教程,我跟着视频(用RSpec测试)一直到后者。我就像在第二章里瘦了一样。我是不是遗漏了什么?该视频是否使用较旧版本的rspec作为视频教程?
在迁移文件中:
class CreateZombies < ActiveRecord::Migration
def change
create_table :zombies do |t|
t.timestamps
end
end
end发布于 2014-11-05 18:15:36
我认为您遗漏了name属性。以下迁移文件将向僵尸模型添加名称属性:
class AddNameToZombies < ActiveRecord::Migration
def change
add_column :zombies, :name, :string
end
end最后运行以下命令:
rake db:migrate
rake db:test:prepare就是这样
https://stackoverflow.com/questions/26753487
复制相似问题