【问题标题】:Can't Execute $ time rspec spec/requests/static_pages_spec.rb无法执行 $ time rspec spec/requests/static_pages_spec.rb
【发布时间】:2015-08-16 08:15:44
【问题描述】:

我正在关注 Michael Hartl 的 Ruby on Rails 示例教程,但我卡住了。

每次我尝试运行以下测试时都会出错:

time rspec spec/requests/static_pages_spec.rb 

我的错误:

/home/i/.rvm/gems/ruby-2.0.0-p643@railstutorial_rails_4_0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1327:in `load': cannot load such file -- /home/i/rails_projects/sample_app/spec/spec/requests/static_pages_spec.rb (LoadError)
from /home/i/.rvm/gems/ruby-2.0.0-p643@railstutorial_rails_4_0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1327:in `block in load_spec_files'
from /home/i/.rvm/gems/ruby-2.0.0-p643@railstutorial_rails_4_0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `each'
from /home/i/.rvm/gems/ruby-2.0.0-p643@railstutorial_rails_4_0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `load_spec_files'
from /home/i/.rvm/gems/ruby-2.0.0-p643@railstutorial_rails_4_0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:102:in `setup'
from /home/i/.rvm/gems/ruby-2.0.0-p643@railstutorial_rails_4_0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:88:in `run'
from /home/i/.rvm/gems/ruby-2.0.0-p643@railstutorial_rails_4_0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:73:in `run'
from /home/i/.rvm/gems/ruby-2.0.0-p643@railstutorial_rails_4_0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:41:in `invoke'
from /home/i/.rvm/gems/ruby-2.0.0-p643@railstutorial_rails_4_0/gems/rspec-core-3.3.2/exe/rspec:4:in `<top (required)>'
from /home/i/.rvm/gems/ruby-2.0.0-p643@railstutorial_rails_4_0/bin/rspec:23:in `load'
from /home/i/.rvm/gems/ruby-2.0.0-p643@railstutorial_rails_4_0/bin/rspec:23:in `<main>'
from /home/i/.rvm/gems/ruby-2.0.0-p643@railstutorial_rails_4_0/bin/ruby_executable_hooks:15:in `eval'
from /home/i/.rvm/gems/ruby-2.0.0-p643@railstutorial_rails_4_0/bin/ruby_executable_hooks:15:in `<main>'

real    0m0.752s
user    0m0.699s
sys 0m0.044s

static_pages_spec.rb:

require 'spec_helper'
 
describe "StaticPages" do
 
  describe "Home page" do
    it "It should have the content 'Sample App'" do
      visit '/static_pages/home'
      expect(page).to have_content('Sample App')
    end
    it "should have the right title" do
          visit '/static_pages/home'
          expect(page).to have_title("Ruby on Rails Tutorial Sample App | Home")
        end
  end
 
describe "Help page" do
    it "It should have the content 'Help'" do
      visit '/static_pages/help'
      expect(page).to have_content('Help')
    end
    it "should have the right title" do
          visit '/static_pages/help'
          expect(page).to have_title("Ruby on Rails Tutorial Sample App | Help")
        end
  end
 
describe "About page" do
    it "It should have the content 'About Us'" do
      visit '/static_pages/about'
      expect(page).to have_content('About Us')
    end
  end
    it "should have the right title" do
          visit '/static_pages/about'
          expect(page).to have_title("Ruby on Rails Tutorial Sample App | About Us")
        end
describe "Contact" do
    it "It should have the content 'Contact'" do
      visit '/static_pages/contact'
      expect(page).to have_content('Contact')
    end
  end
    it "should have the right title" do
          visit '/static_pages/contact'
          expect(page).to have_title("Ruby on Rails Tutorial Sample App | Contact")
        end
 
end

Gemfile: ## http://pastebin.com/1RhbaCiK

操作系统:Ubuntu 14.04

我一直在寻找解决方案,但没有找到。请记住,我是 Ruby on Rails 和 Web 开发的新手。

【问题讨论】:

  • 请出示spec_helper.rb
  • 是的,总是从那里
  • 我试图解决这个问题,但又得到了一个。现在,如果我运行该命令,我会得到: :~/rails_projects/sample_app$ time rspec spec/requests/static_pages_spec.rb /home/i/.rvm/gems/ruby-2.0.0-p643@railstutorial_rails_4_0/gems/guard- spork-1.5.0/lib/guard/spork.rb:2:in `require': 无法加载这样的文件 --guard/guard (LoadError)
  • 下面答案中的解决方案。在 GemfileGemfile.lock 中更新 gem 'guard-spork' - 从 '1.5.0' 到 '2.1.0'

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


【解决方案1】:

你的错误是:

cannot load such file -- /home/i/rails_projects/sample_app/spec/spec/requests/static_pages_spec.rb (LoadError)

这意味着 RSpec 找不到规范。我看到你在文件路径中有两次spec../sample_app/spec/spec/..

您应该将规范保存在 Rails 应用程序的 spec 目录下。所以,路径应该是:/sample_app/spec/ 这样。修复它,然后重试。它应该可以工作。

更新

所以,在本地运行您的代码后,我发现了问题。 gem guard-spork 存在 gem 依赖问题。

你只需要升级这个宝石。在你的Gemfile:

gem 'guard-spork', '1.5.0' 更改为gem 'guard-spork', '2.1.0'

那么,bundle install

然后,运行time rspec

一切都会顺利进行:-)

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-15
  • 1970-01-01
相关资源
最近更新 更多