2

我正在尝试围绕我正在从事的新 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"但没有成功。

有谁知道我做错了什么?

4

1 回答 1

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如果您使用的是 watchr 或其他一些机制,您可能能够为您完成这项工作。

于 2010-03-30T14:22:16.633 回答