rspec& rails 3找不到模型对象

时间:2010-03-30 13:16:36

标签: ruby-on-rails ruby rspec ruby-on-rails-3

我正试图在我正在研究的新rails 3项目中加入一些规范,而我的第一个测试似乎无法找到模型。

我使用以下命令从命令行安装了rspec:

sudo gem install rspec --pre

然后我将以下内容放入我的Gemfile

gem "rspec-rails", ">= 2.0.0.beta.1"

但是当我进行测试时,我得到了

./spec/models/world_spec.rb:1: uninitialized constant World (NameError)
rake aborted!
Command /opt/local/bin/ruby  -Ilib -Ispec "./spec/models/world_spec.rb" failed
/opt/local/lib/ruby/gems/1.8/gems/rspec-core-2.0.0.beta.4/lib/rspec/core/rake_task.rb:71:in 'define'
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:1112:in 'verbose'
/opt/local/lib/ruby/gems/1.8/gems/rspec-core-2.0.0.beta.4/lib/rspec/core/rake_task.rb:57:in 'send'
/opt/local/lib/ruby/gems/1.8/gems/rspec-core-2.0.0.beta.4/lib/rspec/core/rake_task.rb:57:in 'define'
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in 'call'
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in 'execute'
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in 'each'
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in 'execute'
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:597:in 'invoke_with_call_chain'
/opt/local/lib/ruby/1.8/monitor.rb:242:in 'synchronize'
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:590:in 'invoke_with_call_chain'
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:583:in 'invoke'
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2051:in 'invoke_task'
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in 'top_level'
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in 'each'
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in 'top_level'
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in 'standard_exception_handling'
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2023:in 'top_level'
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2001:in 'run'
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in 'standard_exception_handling'
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:1998:in 'run'
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/bin/rake:31
/opt/local/bin/rake:19:in 'load'
/opt/local/bin/rake:19

我的规格在spec / models / world_spec.rb中,看起来像

    describe World, "#hello" do
      it "should be invalid" do
        World.new.should be_invalid?
      end
    end

我尝试添加require "app/model/world"require "world"这样的行,但没有成功。

有谁知道我做错了什么?

1 个答案:

答案 0 :(得分:4)

在rails 3中似乎我需要require 'spec_helper'

require 'spec_helper'

describe World do
  it "should be invalid" do
    World.new.should be_invalid?
  end
end

但这可能只是因为我正在使用rake spec运行它,如果您正在使用监视器或其他一些机制,您可以为您完成这项工作。