我正在关注ruby.railstutorial.org的Rails教程,但不幸的是我现在很难过。我在第3章,它涉及rspec测试。运行以下代码后
require 'spec_helper'
describe "Static pages" do
describe "Home page" do
it "should have the content 'Sample App'" do
# Run the generator again with the --webrat flag if you want to use webrat methods/matchers
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 Sample App |Home')
end
describe "Help page" do
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 Sample App |Help')
end
describe "About page" do
it "should have the content 'About us'" do
visit '/static_pages/about'
expect(page).to have_content('About us')
end
it "should have the right title" do
visit '/static_pages/about'
expect(page).to have_title('Ruby on Rails Sample App |About')
end
end
我将以下命令传递到终端窗口
bundle exec rspec spec/requests/static_pages_spec.rb
我收到了以下输出
/Users/name/.rvm/gems/ruby-2.1.0/gems/rspec-core- 2.13.1/lib/rspec/core/configuration.rb:819:in `load': cannot load such file -- /Users/name/MyProjects/sample_app/app/views/spec/requests/static_pages_spec.rb (LoadError)
from /Users/name/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `block in load_spec_files'
from /Users/name/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `each'
from /Users/name/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load_spec_files'
from /Users/name/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:22:in `run'
from /Users/name/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:80:in `run'
from /Users/name/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:17:in `block in autorun'
规范在使用此代码之前工作正常
require 'spec_helper'
describe "Static pages" do
describe "Home page" do
it "should have the content 'Sample App'" do
visit '/static_pages/home'
expect(page).to have_content('Sample App')
end
end
describe "Help page" do
it "should have the content 'Help'" do
visit '/static_pages/help'
expect(page).to have_content('Help')
end
end
describe "About page" do
it "should have the content 'About Us'" do
visit '/static_pages/about'
expect(page).to have_content('About Us')
end
end
end
这是我的宝石文件(感谢昨天帮助我的人)
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.4'
group :development, :test do
gem 'sqlite3', '1.3.8'
gem 'rspec-rails', '2.13.1'
end
group :test do
gem 'selenium-webdriver', '2.35.1'
gem 'capybara', '2.1.0'
end
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.2'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.2'
group :doc do
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', require: false
end
group :production do
gem 'pg', '0.15.1'
gem 'rails_12factor', '0.0.2'
end
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano', group: :development
# Use debugger
# gem 'debugger', group: [:development, :test]
基本上,我不知道这个输出意味着什么。我也不知道为什么rspec没有对我的文件进行测试。任何人都可以帮我解决这个问题吗?
编辑:有没有办法在这里编写代码而不缩进每行的4个空格?这需要一段时间
答案 0 :(得分:0)
这听起来像你的工作目录,你的Rails根目录混淆了。 RSpec规范应该在:RAILS_ROOT/spec/...
错误表明无法加载:sample_app/app/views/spec/requests/static_pages_spec.rb
您的规范似乎在RAILS_ROOT/app/view/spec
而非RAILS_ROOT/spec
。
运行rspec
时,请确保您的工作目录是rails root。