I am using the Devise Gem in version 3.2.2 for authentication in my Rails 3.2.17 app. Now I am building an API and securing it with devise using the method suggested in this Gist.
Now when I am doing a get request on a secured controller using curl (in this case with a UI) I get a 200 and the response I was looking for. For some reasons my tests fail and produce an error like this:
1) Api::V1::UsersController#information should get the dancing partner
Failure/Error: get :information, {}
NoMethodError:
undefined method `user' for nil:NilClass
# ./app/controllers/api/v1/api_controller.rb:26:in `authenticate_user_from_token!'
# ./spec/controllers/api/users_controller_spec.rb:24:in `block (3 levels) in <top (required)>'
I don't know if this is an issue caused by devise or by something else, so I attached all changed code at the bottom of this post.
Here is an example how it should work:
This is my Api controller, which should secure the normal controllers used for the api.
This is my secured controller
Here are my tests
This is my authentication helper
This is my test helper
The complete output of log/test.log
I just figured out, that this error came from accessing the user variable at a point, where I was testing the response for an invalid authentication.
Related
In my plugin for Discourse I am trying to send an email to a user.
This works just fine if I hardcode my own API functionality to do so, however in an effort to make this public I was trying to use the built in emailing. I cannot however get an email to send.
I have tried:
def self.send_user_email(address)
message = Mail::Message.new(from: "from#fromEmail.com", to: address, body: "hello")
Email::Sender.new(message, :admin_login).send # I used admin_login as according to the spec it should send even if email is disabled.
end
When I do this however I am getting an error:
NoMethodError - undefined method `deliver_now' for #<Mail::Message:0x007fa1bf157300>
Did you mean? deliver:
mail (2.6.6) lib/mail/message.rb:1379:in `method_missing'
So if I go and update line 184 of sender.rb in Discourse to use #deliver instead of #deliver_now, I am able to get it to kinda try to send an email on my development environment.
Rails 4.2 deprecated deliver / deliver! in favor of deliver_now / deliver_now!. (Pull Request)
http://guides.rubyonrails.org/4_2_release_notes.html#action-mailer-deprecations
Rails 5.0 removed deprecated deliver and deliver! methods. (commit)
http://guides.rubyonrails.org/5_0_release_notes.html#action-mailer-removals
This is the reason why you are getting the error. Rails applications >= 5.0 will have this error until the source code you referred to is updated.
I'm trying to make my tests pass, but cucumber is failing with a very confusing error, probably template related, that I'm not able to understand and fix.
Here is my Gemfile and Gemfile.lock:
https://gist.github.com/ngw/dada0c0658a9ef8b5573
You can assume that everything is on its latest version as of today, 28 March 2015, including rails, cucumber, ruby (2.2.0p0), database_cleaner, and so on.
The view that fails to render is a devise view that renders just fine on the browser.
Here is the exception raised:
Background: # features/users/sign_up.feature:6
Given I am not logged in # features/step_definitions/user_steps.rb:9
Scenario: User signs up with valid data # features/users/sign_up.feature:9
When I sign up with valid user data # features/step_definitions/user_steps.rb:13
undefined method `[]' for nil:NilClass
(in /Users/ngw/petswantpats/app/assets/stylesheets/application.css) (ActionView::Template::Error)
./app/views/layouts/application.html.haml:6:in `_app_views_layouts_application_html_haml___302582815389802583_70306431437360'
./features/step_definitions/user_steps.rb:6:in `sign_up'
./features/step_definitions/user_steps.rb:15:in `/^I sign up with valid user data$/'
./features/support/database_cleaner.rb:11:in `block in <top (required)>'
features/users/sign_up.feature:10:in `When I sign up with valid user data'
Then I should see a successful sign up message # features/step_definitions/user_steps.rb:18
Here are the incriminated views:
https://gist.github.com/ngw/95c0a0e6db86cfa0357f
Here is the content of application.css.scss:
https://gist.github.com/ngw/ab766e0cfb5167d2d76a
The failing step:
def sign_up
visit '/users/sign_up'
end
Is it something JS related? I'm using poltergeist with capybara, my env.rb has:
require 'capybara/poltergeist'
Capybara.javascript_driver = :poltergeist
If I use launchy to open it in the browser I have a white page (???) and no other usable information in my test.log...
Can someone help me on how to debug this? TIA
I've created a Rails 4 app with a mountable Rails 4 engine called Foobar. That engine contains a scaffold for Items.
After setting up rspec-rails in the engine, I am unable to get the view specs to pass.
I get the following error:
2) foobar/items/index renders a list of items
Failure/Error: render
ActionView::Template::Error:
undefined method `item_path' for #<#<Class:0x007fe6dae7b3c8>:0x007fe6da9a2ec8>
# ./app/views/foobar/items/index.html.erb:21:in `block in ___sers_ianneub__ocuments__ode_rails_engine_spec_test_engines_foobar_app_views_foobar_items_index_html_erb__1719273292435665376_70314743713380'
# ./app/views/foobar/items/index.html.erb:16:in `each'
# ./app/views/foobar/items/index.html.erb:16:in `___sers_ianneub__ocuments__ode_rails_engine_spec_test_engines_foobar_app_views_foobar_items_index_html_erb__1719273292435665376_70314743713380'
# ./spec/views/foobar/items/index.html.erb_spec.rb:20:in `block (2 levels) in <top (required)>'
When starting up the rails server in either the outer application or the engine's spec/dummy app the views all seem to work just fine.
So my question is:
Why do the rspec tests fail, even though the engine views work fine from the rails server?
Bonus question:
What can I change in the index.html.erb to make the test pass?
I'm really trying to understand what is at play here in the rspec tests. IMHO it seems like there is an issue with rspec (and/or rspec-rails) that prevents this from working. Like something isn't being loaded in the tests that would normally make this work inside the rails server. In other words: if it works in the rails server, then it should work in rspec the same way. Would that be correct? What am I missing?
The app is available here:
https://github.com/ianneub/rails-engine-spec-test
You should be able to clone the repo and run these commands to duplicate the error:
cd engines/foobar
bundle install
rspec
Thank you in advance for any help and guidance that you share with me (and the world).
Line 21 in the 'index.html.erb' view needs scoping to the foobar gem.
<td><%= link_to 'Show', foobar.item_path(item) %></td>
That's because Rspec renderer do not include url_helpers methods, but rails renderer somehow included these methods. Could be fixed by
a before hook:
RSpec.configure do |config|
config.before(:example, type: :view) do
view.class_eval do
include <Your Engine Namespace>::Engine.routes.url_helpers
end
end
end
I'm trudging through this deeply error-prone tutorial on Ruby on Rails located here: http://ruby.railstutorial.org/ruby-on-rails-tutorial-book.
I've been working through a section about testing using rspec. Now, the instructions that this tutorial provided created a whole host of errors (deprecations, array issues, etc.) that filled up my page. After rummaging the internet for several hours, I decided to follow several suggestions to update all my gems.
Having updated my gems and attempted to perform this very basic test (the default test really), I got this whole pile of error that I couldn't begin to understand. All I can say is "please help".
Thank you.
> bundle exec rspec spec/requests/static_pages_spec.rb
Rack::File headers parameter replaces cache_control after Rack 1.5.
←[31mF←[0m
Failures:
1) StaticPages GET /static_pages works! (now write some real specs)
←[31mFailure/Error:←[0m ←[31mget static_pages_path←[0m
←[31mNameError:←[0m
←[31mundefined local variable or method `static_pages_path' for #<RSpec::
Core::ExampleGroup::Nested_1::Nested_1:0x5168040>←[0m
←[36m # ./spec/requests/static_pages_spec.rb:6:in `block (3 levels) in <top
(required)>'←[0m
Finished in 0.19901 seconds
←[31m1 example, 1 failure←[0m
Failed examples:
←[31mrspec ./spec/requests/static_pages_spec.rb:5←[0m ←[36m# StaticPages GET /st
atic_pages works! (now write some real specs)←[0m
If you upgraded all your gems to the latest, then one issue you probably have is your newer capybara gem no longer looks for your tests in 'spec/requests'. That test needs to be in 'spec/features' now. If there is no 'spec/features' just create it.
Also, capybara will need this line in your 'spec/spec_helper.rb' if it isn't already:
require 'capybara/rspec'
I'm having a few problems with the forgot password system from this tutorial.
My application uses Authlogic for the authentication system and it works perfectly for user login/logout and registrations. However after I followed that tutorial to the letter (password_reset controller renamed to 'reset' and I used my own existing mailer configuration), and tried to reset my test accounts password, I get a "wrong number of arguments 1 for 0" error on my reset controller's create action.
ArgumentError in ResetsController#create
wrong number of arguments (1 for 0)
c:/Ruby/lib/ruby/gems/1.8/gems/actionmailer-2.2.2/lib/action_mailer/base.rb:462:in `password_reset_instructions'
c:/Ruby/lib/ruby/gems/1.8/gems/actionmailer-2.2.2/lib/action_mailer/base.rb:462:in `__send__'
c:/Ruby/lib/ruby/gems/1.8/gems/actionmailer-2.2.2/lib/action_mailer/base.rb:462:in `create!'
c:/Ruby/lib/ruby/gems/1.8/gems/actionmailer-2.2.2/lib/action_mailer/base.rb:455:in `initialize'
c:/Ruby/lib/ruby/gems/1.8/gems/actionmailer-2.2.2/lib/action_mailer/base.rb:392:in `new'
c:/Ruby/lib/ruby/gems/1.8/gems/actionmailer-2.2.2/lib/action_mailer/base.rb:392:in `method_missing'
app/models/user.rb:7:in `deliver_password_reset_instructions!'
app/controllers/resets_controller.rb:12:in `create'
I have stared at my code for a couple of hours, trying various tweaks, Google'd the issue, browsed this site, but I still don't know why this is happening and would appreciate any pointers your guys can provide.
Thanks in advance for your help!
I am using ruby 1.8.6, Rails 2.2.2 and Authlogic 2.1.5
If I'm reading that trace properly then it looks to me like password_reset_instructions has been declared as a no-argument method. That's what the (1 for 0) complaint is about. Can you check that you have included the user argument in the definition, as below?
class Notifier < ActionMailer::Base
def password_reset_instructions(user)
end
end
If you've been staring at it for hours it's probably not that simple but worth making sure.