【问题标题】:rspec unit testrspec 单元测试
【发布时间】:2012-07-23 19:14:55
【问题描述】:

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

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

【问题讨论】:

    标签: ruby-on-rails unit-testing rspec rspec-rails


    【解决方案1】:

    首先:

    rails g rspec:install
    

    它将创建文件spec_helper

    然后你可以准备数据库进行测试:

    rake db:test:load
    rake db:test:prepare
    

    【讨论】:

    • 我遇到了与此问题中所述类似的问题,但我没有意识到我需要执行您在此处描述的安装和 rake。谢谢!
    猜你喜欢
    • 2014-09-19
    • 1970-01-01
    • 1970-01-01
    • 2012-02-28
    • 1970-01-01
    • 1970-01-01
    • 2012-11-28
    • 1970-01-01
    • 2023-04-01
    相关资源
    最近更新 更多