Devise route helpers are not known during tests any more - ruby-on-rails

My devise route helpers methods are not recognized in my Ruby functional tests.
For instance, my application's layout contains a call to edit_admin_registration_path (since my device model is Admin) and I get the following error:
ActionView::Template::Error: undefined local variable or method `edit_admin_registration_path' for #<#<Class:0x007ff332374038>:0x007ff330d54988>
when running:
rake test:functionals TEST=test/functional/my_controller_test.rb
I think I get this problem since upgrading to Rails 3.2.15. Any idea why? (devise version is 3.2.2)

Make sure that you have this route helper by:
rake routes
Seems like similar discusion Rails Can't find route in Functional Test

Related

How to move from requests to features in RSpec

I have installed a rspec-rails 3.0.0.beta1 (on ruby2 + rails4) and had some troubles using devise helper in my request specs. After some googling, I've found that I need to move all my specs from spec/requests to spec/features (the requests dir was created by rspec installator or scaffold generator [not sure right now], so I'm a bit confused). That made my devise helper working but there are more issues instead.
Here are three scenarios:
Spec file is spec/requests/events_spec.rb and dont have any type set
undefined method 'visit' for #<RSpec::ExampleGroups::Events::GETEvents:0x007ff2464d9848>
Spec file is spec/requests/events_spec.rb and has a type: :controller
it throws an error undefined method 'events_path' for nil:NilClass when i'm trying to use a get events_path method(s)
Spec file is spec/features/events_spec.rb and dont have any type set
undefined method `get' for #<RSpec::ExampleGroups::Events::GETEvents:0x007ffb2714a968>
Spec file is spec/features/events_spec.rb and have a type: :controller
undefined method `events_path' for nil:NilClass
I think I can find some tweaks on the internet but I'm a fresh rspec user and I feel like I'm doing something extremely wrong. And all the examples online are not related to my problem.
The code is here: https://gist.github.com/mbajur/8002303
As of Capybara 2.0, the Capybara methods (e.g. visit) are only available by default for feature specs, not request specs or controller specs.
Similarly, the get method is not available for feature specs, only controller and request specs.
Finally, the path helper methods are only available in request or feature specs.
Given that, your failures can be explained as follows:
No Capybara, because it's not a feature spec.
No path helper methods because it's a controller spec.
No get method because it's a feature spec.
No path helper method because it's a controller spec.
You need to either:
Make it a request spec and configure RSpec to include Capybara::DSL for request specs
Leave it a feature spec and stop using methods like get which aren't intended for use with feature specs
Here's some interesting background on introducing feature specs as distinct from request specs

Result of `rake routes` in ruby script

rake routes is very slow (30s in my computer) but I need it for routing spec.
So, is there a way to get all routes like rake routes? (or how rake routes works?)
I use Rails 3 and all I have seen is for Rails 3, and I found nothing I can use in the rails doc.
Rails.application.routes.routes.to_a
.to_a is optional, it just converts it to an array.
(I found this line in railties/lib/rails/tasks/routes.rake)
I use it like :
routes[10].defaults => {:action=>"edit", :controller=>"polls"}
Edit : You can find the (quite hacky) way I do my routing specs here : https://gist.github.com/52ac6d848ce0d9fd52ac
If you're using RSpec, you can use routing specs in your tests.
Another option is the rake shell; I love it.

How to access a namespace method in Ruby console?

Found this post Include namespace in Rails 3.1 console but it doesn't seem to work.
The following lib/task defined and it works from the command line: rake namespace_name:task_name.
How to call a method method_name in namespace_name from within the console, without calling the task?
rails console
namespace_name::task_name
NameError: undefined local variable or method 'namespace_name' for main:Object
irb namespace_name
NameError: undefined local variable or method 'namespace_name' for main:Object
Working in Rails 3.07, Ubuntu.
If you want to call a method defined inside a .rake file you do something similar to what #Nate said, but instead of calling the raketask, call the method:
require 'rake'
Rake.load_rakefile 'lib/tasks/namespace_name.rake'
method_name(arg1, arg2)
It feels kind of strange that you don't need to specify the namespaces but I just tried this and it worked.
You're confusing two different kinds of "namespaces" - Ruby modules can perform the task of "namespacing" Ruby code; Rake namespaces are only used within Rake to categorize tasks, and they don't create a module namespace.
The page you linked only works with Ruby module namespaces.
If you want to call Rake tasks from the Rails console, it's a bit more involved...
require 'rake'
Rake.load_rakefile 'lib/tasks/namespace_name.rake'
Rake::Task['namespace_name:task_name'].invoke
Or just call it on the command line from within the Rails console -
%x[rake namespace_name:task_name]

problem accessing namespaced class in rake task

Given a rake task that references both a namespaced and non-namespaced model:
namespace :thing do
task :thingo => :environment do
Klass.first.some_method
Namespaced::Klass.first.some_other_method
end
end
Using ruby 1.9.2, rails 3.0.9, and rake 0.9.2, this yields an exception, like so:
undefined method 'some_other_method' for #<Klass:0x007fcfafbaa6e0>
Two things:
Why doesn't rails return the proper namespacing in the rake environment (in a debugger session), but it does in a console session?
Why does changing the order of reference work? (That is, if the environment is already calling "Namespaced::Klass" as "Klass", then calling "Klass" should fail with undefined method 'some_method' for #<Klass:0x007fcfafbaa6e0> right?
By the way, I've tried ::Namespaced::Klass.first.some_other_method
If the answer isn't simple, I'll put together a test app - let me know! :-)
First, some background on metaphor shear - two different kinds of namespaces:
Although Rake Namepsaces and Ruby Namespaces share the word Namespace, they are separate concepts. Rake namespaces are just organizing containers for Rake Tasks, not Ruby namespaces/modules. So code inside your thing:thingo rake task is actually executing at the top-level Ruby namespace.
Second: If Klass is a single class not in a namespace, you can reference it directly. If the class exists as Foo::Klass then you'll need to use the fully-qualified Foo::Klass reference unless the scope of the reference is already within the Foo namespace.
Because Rake namespaces aren't Ruby modules, you are not in the context of a Ruby namespace within your task. This is why Klass.some_method works if Klass isn't in a module.
If this doesn't explain the question, please post the class definition for Klass including any module/namespace membership.

In Rails, how to see all the "path" and "url" methods added by Rails's routing? (update: using Rails console)

[update: by not using rake routes, just to understand Rails console a little more]
It seems like inside of "rails console" for Rails 3, we can use controller, but in Rails 2.2 or 2.3, we need to use #controller
And in Rails 3, we can print out all the routes added by Rails routing for a scaffold foo:
ruby-1.9.2-p0 > puts controller.public_methods.grep(/path|url/).grep(/foo/).sort.join("\n")
edit_foo_path
edit_foo_url
foo_path
foo_url
foos_path
foos_url
new_foo_path
new_foo_url
but on Rails 2.3.8, it gives a bunch of formatted_foos_path, etc, and gives nothing for Rails 2.2.2. How to make it print out for Rails 2.3.8 and 2.2.2?
Details for Rails 2.3.8:
ruby-1.8.7-p302 > puts #controller.public_methods.grep(/path|url/).grep(/foo/).sort.join("\n")
formatted_edit_foo_path
formatted_edit_foo_url
formatted_foo_path
formatted_foo_url
formatted_foos_path
formatted_foos_url
formatted_new_foo_path
formatted_new_foo_url
Rails 3.x–6.x
Rails.application.routes.named_routes.helper_names
Rails 2.x
helpers = Rails.application.routes.named_routes.helpers
This will get you all the named route methods that were created. Then you can do helpers.map(&:to_s), and whatever regex you want to get your foo versions
or load up localhost_path/rails/info/routes in your browser.
Well in Rails 4, I use rake routes. Is it that you need?
Rails >= 6.1
I just upgraded an app to 6.1.4 and noticed that rake routes no longer works.
Instead we can run bin/rails routes
To get a complete list of the available routes in your application, visit http://localhost:3000/rails/info/routes in your browser while your server is running in the development environment. You can also execute the bin/rails routes command in your terminal to produce the same output.
Docs: https://guides.rubyonrails.org/routing.html#listing-existing-routes

Resources