【发布时间】:2014-11-05 09:23:57
【问题描述】:
我正在自学 RSpec (v3.1.7)。我已将带有rails g rspec:install 的 rspec 安装到现有的 Rails 应用程序中 - 新创建的。
我创建了一个模型:rails g rspec:model zombie。运行迁移,一切顺利。
在:app/models/zombie.rb:
class Zombie < ActiveRecord::Base
validates :name, presence: true
end
在:app/spec/models/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 我得到:
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 测试)一直到后者。我喜欢在第2章减肥。我错过了什么吗?该视频是否使用旧版本的 rspec 作为视频教程?
在我的迁移文件中:
class CreateZombies < ActiveRecord::Migration
def change
create_table :zombies do |t|
t.timestamps
end
end
end
【问题讨论】:
-
我创建了一个
attr_accessor :name,它可以正常工作。但是视频没有attr_accessor!我迷路了!
标签: ruby-on-rails-4 rspec rspec-rails rspec3