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

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.

Related

Rails jbuilder error: ActionView::Template::Error (undefined method

I have a Rails 6 app and we're using jbuilder to define the shape of JSON responses.
Recently, I started getting the following error:
ActionView::Template::Error (undefined method `empty?' for #<Account:0x0000000116743030>):
The stack trace points to this block inside a jbuilder file:
json.account do
json.call(#account, *Account::APP_FIELDS)
json.logo_url #account.logo
end
If I comment out both of the lines inside the block, the error goes away. If I remove either of the lines and leave the other, the error returns. The stack trace just points me to the first line of the block.
What's going on? How do I fix this?
Figured it out!
Higher in the jbuilder file, a partial is included that adds the same account key. This was causing some kind of name collision.
I stumbled on this by temporarily testing with my key renamed accountt and suddenly it worked. That's when I realized it was conflicting name that was causing the issue. I didn't see it because it was in a different file and the error message was confusing. If your jbuilder file is complicated and you think you're running into this same issue, this is an easy way to test.

When I run a rails test can I make it display only the error message?

I am following along with the Ruby on Rails Tutorial by Michael Hartl and am up to the point of running tests. The test is a simple "supposed to fail" test shown here:
test "should get about" do
get static_pages_about_url
assert_response :success
end
with the error being simply that the about page, controller method, and route do not yet exist. However when I run the test I get a massive block of code which fills up my terminal. This is just a small sampling as an example of what i'm seeing.
E
Error:
StaticPagesControllerTest#test_should_get_about:
NameError: undefined local variable or method `static_pages_about_url' for #<StaticPagesControllerTest:0x00007fe6682cc760 #_routes=nil, #NAME="test_should_get_about", #failures=[#<Minitest::UnexpectedError: Unexpected exception>], #assertions=0, #integration_session=#<#<Class:0x00007fe6685342c8>:0x00007fe66859fc58 #_routes=nil, #app=#<SampleApp::Application:0x000055d7735cea68 #_all_autoload_paths=["/home/clay/Programs/sample_app/app/channels", "/home/clay/Programs/sample_app/app/controllers", "/home/clay/Programs/sample_app/app/controllers/concerns", "/home/clay/Programs/sample_app/app/helpers", "/home/clay/Programs/sample_app/app/jobs", "/home/clay/Programs/sample_app/app/mailers", "/home/clay/Programs/sample_app/app/models", "/home/clay/Programs/sample_app/app/models/concerns"], #_all_load_paths=["/home/clay/Programs/sample_app/lib", "/home/clay/Programs/sample_app/vendor", "/home/clay/Programs/sample_app/app/channels", "/home/clay/Programs/sample_app/app/controllers", "/home/clay/Programs/sample_app/app/controllers/concerns", "/home/clay/Programs/sample_app/app/helpers", "/home/clay/Programs/sample_app/app/jobs", "/home/clay/Programs/sample_app/app/mailers", "/home/clay/Programs/sample_app/app/models", "/home/clay/Programs/sample_app/app/models/concerns"], #app=#<ActionDispatch::HostAuthorization:0x000055d774009820 #app=#<Rack::Sendfile:0x000055d774009988 #app=#<ActionDispatch::Static:0x000055d773fe4f98 #app=#<ActionDispatch::Executor:0x000055d773fe5088 #app=#<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x000055d77382f4d8 #name="ActiveSupport::Cache::Strategy::LocalCache", #local_cache_key=:active_support_cache_null_store_local_cache_4420, #app=#<Rack::Runtime:0x000055d773fe51f0 #app=#<Rack::MethodOverride:0x000055d773fe5290 #app=#<ActionDispatch::RequestId:0x000055d773fe5330 #app=#<ActionDispatch::RemoteIp:0x000055d773fe5420 #app=#<Rails::Rack::Logger:0x000055d773fe54e8 #app=#<ActionDispatch::ShowExceptions:0x000055d773fe5588 #app=#<ActionDispatch::DebugExceptions:0x000055d773fe56c8 #app=#<ActionDispatch::ActionableExceptions:0x000055d773fe5790 #app=#<ActionDispatch::Reloader:0x000055d773fe58f8 #app=#<ActionDispatch::Callbacks:0x000055d773fe5998 #app=#<ActionDispatch::Cookies:0x000055d773fe5a38 #app=#<ActionDispatch::Session::CookieStore:0x000055d773fe5c68 #app=#<ActionDispatch::ContentSecurityPolicy::Middleware:0x000055d773fe5d58 #app=#<ActionDispatch::PermissionsPolicy::Middleware:0x000055d773fe5e20 #app=#<Rack::Head:0x000055d773fe5ec0 #app=#<Rack::ConditionalGet:0x000055d773fe5f60 #app=#<Rack::ETag:0x000055d773fe6050 #app=#<Rack::TempfileReaper:0x000055d773fe60f0
All I really need is the NameError: undefined local variable or method 'static_pages_about_url' and the location of the failing test right? Is this standard for failing tests in Ruby on Rails or is there a way to change how error messages are displayed from showing all of this less useful information?
In test driven development (TDD), you write the tests first. When you run them, all of them will be failing because you have not written the implementation yet (this is your "Red"). After that, you write the implementation that makes the tests pass (this is your "Green"). Once all tests are passsing, it comes the stage where you refactor the implementation (if required, this is your "Refactor"). Red, Green, Refactor.
In your particular example, you have written the test that checks if you can "GET" the about page but because you have not yet implemented the solution, your tests are failing.
In order to make the test pass, you'll need to add this to your route.rb file.
get 'static_pages/about'

resque-web: NoMethodError at /failed undefined method `to_yaml'

Resque-web is up and running but when trying to view the failed jobs I get the following error:
NoMethodError at /failed
undefined method `to_yaml' for 3:Fixnum
The stack trace points to a 'to_yaml' call as mentioned in the error, it seems like resque-web is missing a requires. Has anyone else had this problem or know how to solve it?
Note: I'm running this locally on a rails 4 app.
You can see the reason here: https://github.com/resque/resque/issues/1143 -- it boils down to a temporary bug that is fixed, but not released yet. I fixed mine by adding
require 'yaml'
at the top of the server.rb file
note that the server.rb file on my machine was located at:
/usr/local/lib/ruby/gems/2.0.0/gems/resque-1.25.1/lib/resque/server.rb
your location may vary

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.

RSpec Rails printing undefined home_path error

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.

Resources