RSpec Rails printing undefined home_path error - ruby-on-rails

When I run rspec on my rails project I get a very annoying error message, although exact the same code worked before. The only thing I did was adding a new rspec file. Now the new rspec file doesn't contain anything except for "require 'spec_helper'" but I still get this message (and a few others...):
1) Home Page should have 'Lists' and 'Students' links
Failure/Error: visit home_path
NameError:
undefined local variable or method home_path' for > #<RSpec::Core::ExampleGroup::Nested_2:0x00000004c455a0>
# ./spec/requests/homes_spec.rb:5:inblock (2 levels) in '
I have no idea what to do now. Has anyone a clue what's the matter with it?
Thanks

According to your routes proper helper would be home_index_path.
Try to use it.

Related

Rails integration test failing on Class.count - nil.nill.NIL

ENV=development
Im getting a weird error that makes no sense to me
when i run an integration test with Myclass.count I get error
ERROR["test_micropost_interface", MicropostsInterfaceTest, 2016-01-20 23:50:17 +0000]
test_micropost_interface#MicropostsInterfaceTest (1453333817.28s)
ActionView::Template::Error: ActionView::Template::Error: undefined method `count' for nil:NilClass
the code is and was working im sure !
assert_no_difference 'Micropost.count' do
post microposts_path, micropost: { content: "" }
end
I ran a console and Micropost.count returns 32
Is this possibly an issue with the fixtures not propagating ?
im stumped :( take advice on where to look !
I think the error is coming from within one of your views rather than being a problem with the test itself.
It's probably worth removing the silencing on backtraces and seeing if you get a more detailed error which will hopefully point to the error more accurately.
In config/initializers/backtrace_silencers.rb you can uncomment the last line to get rails to show the entirety of an error. I often change this line to:
Rails.backtrace_cleaner.remove_silencers! if Rails.env.test?
to get full backtraces when I'm running tests.

Can't create users in rails when using authlogic because of cookies

So, I have a standard rails 3 app with authlogic. If I'm not in the browser (in the console or in the test environment), I can't create user models.
For example:
I have this code either in a rspec test or in my console:
user = User.create(...user attributes...)
And I get this error:
NoMethodError: undefined method `cookies' for main:Object
I've looked all over the internet and can't figure this out. Any help would be greatly appreciated.
EDIT:
So, I looked around some more and found in the documentation to do this:
include Authlogic::TestCase
but then I get this error:
uninitialized constant Authlogic::TestCase
I had this same exact problem. Where in your application did you put the following line:
Authlogic::Session::Base.controller = Authlogic::ControllerAdapters::RailsAdapter.new(self)
I ran into this error when I placed the line in either the environment.rb file or within a file within my initializer folder.
I eliminated the problem when I placed the line in my ApplicationController instead.

Why is RSpec/Capybara not showing where errors occured

I'm using Capybara with webkit for my testing, but for some reason when a test fails it shows the error, but not where it actually occurred in the code.
Failures:
1) online shopping - sign up
Failure/Error: page.should have_content 'Payment added successfully'
expected there to be content "Payment added successfully" in "Internal Server Error undefined method `client_id' for #<InvoicePayment:0x007fbd5b834008> WEBrick/1.3.1 (Ruby/1.9.3/2012-04-20) at 127.0.0.1:60324"
# ./spec/requests/online_shopping_spec.rb:140:in `block (2 levels) in <top (required)>'
and when using save_and_open_page it'll just show the error, with no information on where it occured:
Internal Server Error
undefined method `client_id' for #
WEBrick/1.3.1 (Ruby/1.9.3/2012-04-20) at 127.0.0.1:60324
What I'm expecting to see is the line number and function where the error occured:
app/controllers/invoices_controller.rb:30:in `show'
I can't seem to find anything related to this on Google. I'm probably using incorrect nomenclature. Anybody know how to fix this?
Basically capybara does not have a knowledge about the app, since it's running in the different process. You could workaround this problem using this a trick described here https://gist.github.com/1443408
The problem is that the actual page is not rendering because of an error, and instead you are getting an internal server error. So Internal Server Error undefined method... is the content of the page you are testing. RSpec/Capybara can't tell you where it occurred because the test framework only tests what you actually see on the page, and that is exactly what you see (as you confirmed when you ran save_and_open_page).
To track down the error you should look at your rails error log, or the console/terminal where you are running it from. Without more information I can't help you track down the error.
Hope that helps.

testing rails app with cucumber and capybara won't recognize current_path.should == new_session_path

I'm trying to make a simple assertion on expected path.
in my step definition file, according to the capybara docs:
Then /^I should be on the login page/ do
current_path.should == new_session_path
end
this returns
undefined method `new_session_path' for #<Cucumber::Rails::World:0x0000010340b4c0> (NoMethodError)
It looks as though it's not loading the route helpers..
This was a brain-fart issue. Really sorry about that. A detail that eluded me for a long time was that I was running into rails' engine isolation issue. Specifically I was testing refinerycms and running into the problem described here: https://github.com/resolve/refinerycms/issues/1259
so to fix it was dead simple:
current_path.should == refinery.admin_root_path

Undefined local variable or method 'page' for Cucumber::Rails::World (NameError)

I'm running a basic feature by following RBates RailsCasts tutorial using Rspec 2.5.0 and Cucumber-rails 0.4.1 on a cygwin environment. I am at the step where I am testing "Then I should see"
For example:
Scenario: Stores List
Given I have stores named Pizza, Breadsticks
When I go to the list of stores
**Then I should see "Pizza"**
Running cucumber features gives me the following error message:
Undefined local variable or method 'page' for Cucumber::Rails::World (NameError)
Then I should See is defined in the web_steps file as follows:
if page.respond_to? :should
page.should have_content(text)
else
assert page.has_content?(text)
end
Any guidance would be appreciated!
Thank you!
Fixed the error. I had commented out:
Capybara.default_selector = :css because of a previous issue (See: https://github.com/aslakhellesoy/cucumber-rails/issues/120). Once i included the following:
require 'capybara/rails'
require 'capybara/cucumber'
It fixed the capybara issue and the page method was available.
Thanks.
I don't know much of RoR, and without seeing more code, it appears you haven't defined the variable 'page', or you defined it in an area that is outside the scope of where you're trying to use it.

Resources