代码之家  ›  专栏  ›  技术社区  ›  Ceilingfish

rspec&rails 3找不到模型对象

  •  2
  • Ceilingfish  · 技术社区  · 16 年前

    我正试着在一个新的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 回复  |  直到 16 年前
        1
  •  4
  •   Ceilingfish    16 年前

    似乎在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或其他机制,你也许可以做到这一点。