Rspec和Rails for Zombies测试不起作用



我正在学习僵尸课程的rspec rails,我在运行测试时遇到了问题,因为他们没有读取我的App/models文件夹中的ruby代码。我甚至试着把我称之为zombie.rb的ruby文件放进spec文件夹本身和require_relative中,但测试仍然失败,有人能帮我吗。我是一个新手,我发现TDD是学习专业代码的最好、最快的方法。我的代码如下,我在zombie_spec.rb文件和zombie.rb文件中分别有什么:

require_relative 'spec_helper'
require_relative 'zombie'
describe Zombie do
  it 'is invalid without a name' do
    zombie = Zombie.new
    zombie.should_not be_valid
    end
   it 'include tweets' do
     tweet1 = Tweet.new(status: 'Uuuuunhhhhh')
     tweet2 = Tweet.new(status: 'Arrrrggggg')
     zombie = Zombie.new(name: 'Ash', tweets: [tweet1, tweet2])
     zombie.tweets.should include(tweet1)
     zombie.tweets.should include(tweet2)
     end
 end

和zombie.rb文件在这里

class Zombie < ActiveRecord::Base
attr_accessor :Tweet
validates :name, presence: true
end

这是我得到的测试结果

1) Zombie is invalid without a name
     Failure/Error: zombie = Zombie.new
     ActiveRecord::StatementInvalid:
       Could not find table 'zombies'
     # ./zombie_spec.rb:8:in `block (2 levels) in <top (required)>

2) Zombie include tweets
     Failure/Error: tweet1 = Tweet.new(status: 'Uuuuunhhhhh')
     NameError:
       uninitialized constant Tweet
     # ./zombie_spec.rb:15:in `block (2 levels) in <top (required)>'

这看起来像是在查找Zombie模型,因为您从ActiveRecord收到了一个错误,它抱怨在数据库中找不到zombies表,这表明您还没有完成数据库迁移(或有问题)。

第二个错误表示您不需要任何定义Tweet的文件。

最新更新