1

当我开始使用$ bundle exec rspec spec/RSpec 进行单元测试时...

admin@ADMINPC /d/ruby/rails/projects/CMS (static-pages)
$ bundle exec rspec spec/
d:/ruby/rails/projects/CMS/spec/controllers/pages_controller_spec.rb:1:in `requi
re': cannot load such file -- spec_helper (LoadError)
        from d:/ruby/rails/projects/CMS/spec/controllers/pages_controller_spec.r
b:1:in `<top (required)>'
        from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
1.1/lib/rspec/core/configuration.rb:780:in `load'
        from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
1.1/lib/rspec/core/configuration.rb:780:in `block in load_spec_files'
        from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
1.1/lib/rspec/core/configuration.rb:780:in `map'
        from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
1.1/lib/rspec/core/configuration.rb:780:in `load_spec_files'
        from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
1.1/lib/rspec/core/command_line.rb:22:in `run'
        from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
1.1/lib/rspec/core/runner.rb:69:in `run'
        from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
1.1/lib/rspec/core/runner.rb:8:in `block in autorun'

如何解决这个问题?

文件 pages_controller_spec.rb:

require 'spec_helper'

describe PagesController do

  describe "GET 'home'" do
    it "returns http success" do
      get 'home'
      response.should be_success
    end
  end

  describe "GET 'contact'" do
    it "returns http success" do
      get 'contact'
      response.should be_success
    end
  end

end
4

1 回答 1

3

首先:

rails g rspec:install

它将创建文件spec_helper

比您可以准备数据库进行测试:

rake db:test:load
rake db:test:prepare
于 2012-07-23T19:19:29.447 回答