[英]Rspec and named routes
I'm quite new to rails, and trying to follow the railstutorial.我对 rails 很陌生,并试图遵循 railstutorial。 Everything goes fine, except for my tests which can't get past the named routes (5.3.3)一切正常,除了我的测试无法通过命名路线(5.3.3)
My routes.rb :我的路线.rb:
SampleApp::Application.routes.draw do
resources :users
match '/signup', to: 'users#new'
match '/help', to: 'static_pages#help'
match '/about', to: 'static_pages#about'
match '/contact', to: 'pages#contact'
root to: 'static_pages#home'
#Commented stuff
My firsts tests (spec/controllers/static_pages_controller_spec.rb) :我的第一次测试(spec/controllers/static_pages_controller_spec.rb):
describe "Static pages" do
subject { page }
shared_examples_for "all static pages" do
it { should have_selector('h1', text: heading) }
it { should have_selector('title', text: full_title(page_title)) }
end
describe "Home page" do
before { visit root_path }
let(:heading) { 'Sample App' }
let(:page_title) { 'Home' }
it_should_behave_like "all static pages"
end
#Other tests
The spec_helper.rb looks like (without all the commented stuff) spec_helper.rb 看起来像(没有所有评论的东西)
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
end
The errors I get from rspec are all like this one :我从 rspec 得到的错误都是这样的:
Static pages Home page it should behave like all static pages
Failure/Error: before { visit root_path }
NameError:
undefined local variable or method `root_path' for #<RSpec::Core::ExampleGroup::Nested_2::Nested_1::Nested_1:0x00000004a12210>
Shared Example Group: "all static pages" called from ./spec/controllers/static_pages_controller_spec.rb:17
# ./spec/controllers/static_pages_controller_spec.rb:13:in `block (3 levels) in <top (required)>'
I already tried using我已经尝试使用
include Rails.application.routes.url_helpers
in the spec_helper, but it changed my errors to在 spec_helper 中,但它将我的错误更改为
Static pages Home page it should behave like all static pages
Failure/Error: Unable to find matching line from backtrace
SystemStackError:
stack level too deep
# /usr/lib/ruby/1.9.1/forwardable.rb:185
I also tried different way of renaming my routes, but none of them worked.我也尝试了不同的方式来重命名我的路线,但都没有奏效。 I'm back to the tutorial version.我回到教程版本。
If it can be of any help in finding what exactly is a problem, I'm on Ubuntu 11.10, with rails 3.2.1 and ruby 1.9.2p290.如果它可以帮助您找出问题所在,那么我使用的是 Ubuntu 11.10,使用 rails 3.2.1 和 ruby 1.9.2p290。 Hope you can help, I spend quite a while googling for a solution and didn't find any ^^'希望你能帮忙,我花了很长时间在谷歌上搜索解决方案,但没有找到任何 ^^'
Named routes should work if you put the following in rspec_helper.rb:如果将以下内容放入 rspec_helper.rb 中,命名路由应该可以工作:
RSpec.configure do |config|
config.include Rails.application.routes.url_helpers
...
end
Is that how you set it up?你是这样设置的吗?
in rspec_helper.rb: 在rspec_helper.rb中:
RSpec.configure do |config|
config.include Rails.application.routes.url_helpers
...
end
Google brought me here, even my error message doesn't fit 100%.谷歌把我带到这里,即使我的错误信息也不适合 100%。
In my case Capybara command visit
is unknown...在我的情况下,Capybara 命令visit
是未知的......
Error:错误:
NoMethodError:
undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0xa49a73c>
Since Capybara 2.0 one has to use folder spec/features
capybara commands don't work in folder spec/requests
anymore.由于 Capybara 2.0 必须使用文件夹spec/features
capybara 命令不再在文件夹spec/requests
工作。
That helped me: http://alindeman.github.com/2012/11/11/rspec-rails-and-capybara-2.0-what-you-need-to-know.html这对我有帮助: http : //alindeman.github.com/2012/11/11/rspec-rails-and-capybara-2.0-what-you-need-to-know.html
Hope you find this usefull.希望你觉得这很有用。
I don't think you have access to named routes inside of your rspec controller specs.我认为您无法访问 rspec 控制器规范中的命名路由。 You could however just do visit('/'), which is the equivalent of root_path.但是,您可以只执行visit('/'),这相当于root_path。
You should have used你应该用过
rails generate rspec:install
instead of代替
rspec --init
and you wouldn't have had to modify the config file.并且您不必修改配置文件。
Don't do it now though or your application will break and you'll have to waste some more time figuring out why it broke.但是现在不要这样做,否则您的应用程序会崩溃,您将不得不浪费更多时间来弄清楚它为什么会崩溃。
I had the same problem, with the same Tutorial.我有同样的问题,使用相同的教程。 It turns out that I needed to just restart the Spork service, and all worked fine.事实证明,我只需要重新启动 Spork 服务,一切正常。
The solution posted by Tom L worked for me, but when I removed that line and restarted Spork, that also fixed the problem. Tom L 发布的解决方案对我有用,但是当我删除该行并重新启动 Spork 时,也解决了问题。
Hope that helps out any other people who are wary about deviating from the tutorial's code!希望能帮助任何其他对偏离教程代码持谨慎态度的人!
Named routes should work if you put the following in rails_helper.rb not the spec_helper.rb:如果您将以下内容放在rails_helper.rb而不是 spec_helper.rb 中,则命名路由应该可以工作:
checkout at my rails_helper.rb code在我的 rails_helper.rb 代码结帐
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../config/environment', __dir__)
# Prevent database truncation if the environment is production
if Rails.env.production?
abort('The Rails environment is running in production mode!')
end
require 'rspec/rails'
require 'capybara/rails'
RSpec.configure do |config|
config.include Rails.application.routes.url_helpers
config.include Devise::Test::ControllerHelpers, type: :controller
config.include Devise::Test::ControllerHelpers, type: :view
config.include Warden::Test::Helpers
end
begin
ActiveRecord::Migration.maintain_test_schema!
rescue ActiveRecord::PendingMigrationError => e
puts e.to_s.strip
exit 1
end
RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!
# Filter lines from Rails gems in backtraces.
config.filter_rails_from_backtrace!
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.