Can't modify frozen Array error when running rspec - ruby-on-rails

Had been in the process of getting a Rails Engine upgraded to Rails 5.1 and now in the process of getting the rspec tests back working.
I have a controller, and in that controller I have the following:
module Users
class SessionsController < Devise::SessionsController
skip_before_action :authenticate_user!
skip_before_action :authorize_user!
def create
super
flash[:analytics] = { "data-analytics-form-completed-name" => "#{StewardshipUser.app_slug}/sign-in", "data-analytics-form-completed-type" => "login" }
end
def repopulate_email
(params[:user] && params[:user][:email]) ? params[:user][:email] : ''
end
helper_method :repopulate_email
end
end
If I remove the skip_before_action :authorize_user! the tests run, but not all successfully.
With the line I'm getting the following error:
An error occurred while loading ./spec/validators/rfc_compliant_validator_spec.rb.
Failure/Error: Dummy::Application.initialize!
RuntimeError:
can't modify frozen Array
# /Users/ahcarpenter/.rvm/gems/ruby-2.2.2/gems/railties-5.1.4/lib/rails/engine.rb:579:in `unshift'
# /Users/ahcarpenter/.rvm/gems/ruby-2.2.2/gems/railties-5.1.4/lib/rails/engine.rb:579:in `block in <class:Engine>'
Any thoughts on why the initializer would be breaking down with that line in there?
Additionally, when I had initially gone to reinitialize rspec, I had to comment out that method to get the initializer to run as it was not found any longer for some reason (I don't seem to have bumped any gem versions up that contained that method, but maybe so)

I'm sure you've fixed this by now, but I had this error, I realized that my test database was not initialized properly. I fixed the error by running the following command:
rails db:migrate RAILS_ENV=test
I hope this is helpful for anyone who stumbles across this post.

Related

"The action 'execute' could not be found for GraphqlController" when using GraphQL on an API only Rails app?

I've posted a bug for this on rmosolgo/graphql-ruby but just in case I may be doing something wrong, I'm hoping to see if anyone else has a solution to my problem.
When creating an API only rails application it seems that Rails believes the execute method in my GraphqlController is missing.
Here is my graphql_controller.rb file:
class GraphqlController < ApplicationController
# If accessing from outside this domain, nullify the session
# This allows for outside API access while preventing CSRF attacks,
# but you'll have to authenticate your user separately
protect_from_forgery with: :null_session
def execute
variables = ensure_hash(params[:variables])
query = params[:query]
operation_name = params[:operationName]
context = {
# Query context goes here, for example:
# current_user: current_user,
}
result = RailsApiGraphqlExecuteTestSchema.execute(query, variables: variables, context: context, operation_name: operation_name)
render json: result
rescue => e
raise e unless Rails.env.development?
handle_error_in_development e
end
# ... continues on
end
Here is my routes.rb file:
Rails.application.routes.draw do
post "/graphql", to: "graphql#execute"
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end
when I run rake routes this is what I get:
prompt> rake routes
Prefix Verb URI Pattern Controller#Action
graphql POST /graphql(.:format) graphql#execute
... continues on
You should be able to reproduce this with the following command line steps:
rails new execute-test --api
cd execute-test
vim Gemfile # or open an editor and add "gem 'graphql'"
bundle
rake db:create
rails g graphql:install
rake routes # to test that the route exists
rails s
When you use an app like GraphiQL and go to http://localhost:3000/graphql you'll get the following error:
Started POST "/graphql" for 127.0.0.1 at 2019-10-15 16:23:29 -0700
(0.3ms) SELECT sqlite_version(*)
AbstractController::ActionNotFound (The action 'execute' could not be found for GraphqlController):
... strack trace continues ...
Maybe I'm doing something wrong? Any help would be much appreciated.
It looks like this line in the execute method:
protect_from_forgery with: :null_session
is what caused the problem. I'll have to look into this some more. +1 and I'll even mark the answer correct if someone can figure out why this is happening.
Edit: The reason this is happening is because this method assumes you're inheriting from ActionController::Base and not ActionController::API (which doesn't have this method). the API class is supposed to be lighter and therefore doesn't support cookies/sessions out of the box.

Test ActionController::RoutingError: uninitialized constant V1::LocationsController

Continued from uninitialized constant error when running tests on Ruby on rails 5 ...
I'm writing test cases for my api controller with namespace - v1 but it seems ROR doesn't understand the namespace V1 exists.
When I run "rails routes", this is what I get:
v1_locations GET /v1/locations(.:format) v1/locations#index
POST /v1/locations(.:format) v1/locations#create
v1_location GET /v1/locations/:id(.:format) v1/locations#show
PATCH /v1/locations/:id(.:format) v1/locations#update
PUT /v1/locations/:id(.:format) v1/locations#update
DELETE /v1/locations/:id(.:format) v1/locations#destroy
and this is what I have in my test file...
class V1::LocationControllerTest < ActionDispatch::IntegrationTest
def setup()
# A bunch of stuff here
end
test "cannot retrieve a list of locations without a valid token" do
get v1_locations_url
assert_response :unauthorized
end
# More tests here...
end
and this is what my controller looks like
class V1::LocationController < ApplicationController
# To be filled once tests run and fail
end
and this is the error message I'm getting...
Error:
V1::LocationControllerTest#test_cannot_create_a_location_without_a_valid_token:
ActionController::RoutingError: uninitialized constant V1::LocationsController
test/controllers/v1/location_controller_test.rb:32:in `block in <class:LocationControllerTest>'
bin/rails test test/controllers/v1/location_controller_test.rb:31
It's also funny that I get page not found error when I send a GET request to /v1/locations
Mmm... What am I missing here?
Thanks,
Check your error log it says ActionController::RoutingError: uninitialized constant V1::LocationsController, while you controller name is LocationController.
Please rename it to LocationsController and rename a file to locations_controller.rb.
You can get more insights about that here

Rails 5 Integration test fails with NoMethodError: undefined method `[]=' for nil:NilClass when using Devise helper sign_in

I'm writing an integration test for Rails v5.1 using built-in Minitest.
Here's the integration test class:
require 'test_helper'
class PuppiesEndpointsTest < ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers
test "DELETE puppy" do
marty = people(:marty)
sign_in(marty)
# delete puppies_delete_path(marty.puppies.first.id)
# delete `/api/v1/puppies/destroy/${marty.puppies.first.id}.json`
# delete puppies_path(marty.puppies.first.id)
delete '/api/v1/puppies/destroy/6666.json'
assert_response :success
end
end
All of the routes above, including the ones that are commented out, result in the same cryptic error:
Error:
PuppiesEndpointsTest#test_DELETE_puppy:
NoMethodError: undefined method `[]=' for nil:NilClass
test/integration/puppies_endpoints_test.rb:17:in `block in <class:PuppiesEndpointsTest>'
bin/rails test test/integration/puppies_endpoints_test.rb:7
It doesn't give a stack trace or any other information to diagnose what the hell it's talking about. I used byebug to debug the marty variable right before that delete line that's throwing the error. It shows the expected puppies array of associated (fixture) records.
I've also placed a byebug at the very top of the controller action and this error fails the test before it reaches that byebug, so I think that pretty much rules out anything in the action code.
Here's the relevant chunk of what I see when I run rake routes:
PATCH /api/v1/puppies/edit/:id(.:format) puppies#update
DELETE /api/v1/puppies/destroy/:id(.:format) puppies#destroy
puppies_create POST /api/v1/puppies/create(.:format) puppies#create
Here's what is actually in the my routes file:
scope '/api' do
scope '/v1' do
devise_for :people
patch 'puppies/edit/:id' => 'puppies#update'
delete 'puppies/destroy/:id' => 'puppies#destroy'#, as: 'puppies_delete'
post 'puppies/create' => 'puppies#create'
...
I'm completely stumped as to what/why I'm getting this error. The actual code is working completely as expected.
My hunch is that maybe there's a missing config variable that's not getting set for the test environment (I use dotenv gem), but I have no idea how to track that down if the error won't give me any context whatsoever.
UPDATE
I have isolated this problem to using the Devise helper sign_in method. When I remove this method call, the problem goes away.
Here's the problematic test class:
require 'test_helper'
class PuppiesEndpointsTest < ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers
test "do stuff" do
...
app/controllers/api_controller.rb:
class ApiController < ActionController::API
end
Maybe sign_in does not work for testing controllers that do not inherit from ActionController::Base
I changed the controller to inherit from ActionController::Base and nothing changed. I still can't use sign_in without getting that error, but it works find if I "manually" post a request to the sign_in endpoint.
UPDATE 2
I found this Devise issue which sounds related to my problem: https://github.com/plataformatec/devise/issues/2065
Looks like I found the issue. Apparently, in rails-api mode, the ActionDispatch::Cookies and ActionDispatch::Session::CookieStore middlewares are inserted in the end of the middleware stack, which doesn't occur in normal Rails mode.
Due to this, those middlewares are included after Warden::Manager which messes up something in request specs.
Try to set in test.rb
Rails.application.config.middleware.insert_before Warden::Manager, ActionDispatch::Cookies
Rails.application.config.middleware.insert_before Warden::Manager, ActionDispatch::Session::CookieStore

uninitialized constant after capistrano delpoy:cold - nginx, unicorn

I have setup capistrano for deployment with the exact same config/setup as found in the railscasts Pro episode http://railscasts.com/episodes/335-deploying-to-a-vps?view=asciicast.
All of the deploy:check, status and cold tasks run and complete successfully (after some tinkering). However, the app not running and shows the classic "something went wrong" error page. When I check my unicorn.log it shows the error below:
I have tried requiring the module before including it to address threadsafe issues and also autoloading the absolute path in application.rb. Note this all works in development environment.
How can I amend my code to fix this NameError issue?
unicorn.log
E, [2013-10-16T04:15:00.313177 #12996] ERROR -- : uninitialized constant AnswersController::Teebox (NameError)
/home/andrew/rails/teebox/releases/20131016032538/app/controllers/answers_controller.rb:5:in `<class:AnswersController>'
/home/andrew/rails/teebox/releases/20131016032538/app/controllers/answers_controller.rb:1:in `<top (required)>'
answers_controller.rb
class AnswersController < ApplicationController
before_filter :authenticate_user!, except: [:index, :show]
load_and_authorize_resource
require 'teebox/commentable'
include Teebox::Commentable # Offending line
...
end
lib/teebox/commentable.rb
require 'active_support/concern'
module Teebox::Commentable
extend ActiveSupport::Concern
included do
before_filter :comments
end
def comments
#comment = Comment.new
end
end
application.rb
# Custom directories with classes and modules you want to be autoloadable.
config.autoload_paths += %W(#{config.root}/decorators)
config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += %W(#{config.root}/lib/teebox/commentable.rb)
specs:
capistrano 2.15.5
rails 3.2.14
ruby 1.9.3-p488
ubuntu 12.04
If anyone needs more code just shout.
I had an naming convention which wasn't an issue on Mac but was an issue on a case sensitive Linux box.
I had a lib/Teebox/commentable.rb folder structure and was then calling:
include Teebox::Commentable.
So I switched this to lib/teebox/commentable.rb (Lowercase teebox) and this solved the error.

Delayed_job in rails failing

I am just beginning to look into using the delayed_job gem.
To test it, I added "delayed" to the welcome email function and changed that call from
UserMailer.welcome_email(self).deliver
to
UserMailer.delay.welcome_email(self)
This is called inside the User model after_create. I see an entry show up in the delayed_job table after the function executes. Now when I run "rake jobs:work" on command line the task starts but gives errors as below
[Worker(host:Sanjay-PC pid:7008)] Starting job worker
[Worker(host:Sanjay-PC pid:7008)] Class#welcome_email failed with NoMethodError: undefined method `welcome_email' for #<Class:0x4871d60> - 0 failed attempts
[Worker(host:Sanjay-PC pid:7008)] 1 jobs processed at 0.0939 j/s, 1 failed ...
Thinking that if I changed the welcome_email method declaration to a Class method as
def self.welcome_email(user)
(added self. in front) that might help. But then when I run rake jobs:work I get the following error
rake aborted!
undefined method `welcome_email' for class `UserMailer'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/core_ext/module/aliasing.rb:31:in `alias_method'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/core_ext/module/aliasing.rb:31:in `alias_method_chain'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/delayed_job-2.1.4/lib/delayed/message_sending.rb:50:in `handle_asynchronously'
c:/mgn/mgn-r3/app/mailers/user_mailer.rb:10:in `<class:UserMailer>'
c:/mgn/mgn-r3/app/mailers/user_mailer.rb:1:in `<top (required)>'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:454:in `load'
<Stack truncated>
It seems to now know the class as UserMailer but it somehow doesn't see the class method welcome_email.
I am on Rails 3.0.5, Ruby 1.9.2p180 and the installed delayed_job gem is 2.1.4 - on Windows
Can't seem to find any related answers anywhere.
Thanks for your thoughts.
-S
Adding UserMailer code per #pjammer's request
class UserMailer < ActionMailer::Base
default :from => "from#example.com"
def welcome_email(user)
#user = user
#url = "http://example.com/login"
mail(:to => user.email,
:subject => "Welcome to My Awesome Site")
end
end
Just use this
UserMailer.delay.welcome_email(self).deliver
instead of
UserMailer.welcome_email(self).delay.deliver
My solution was to redefine function at the handler class (for you it's UserMailer class)
def self.taguri
'tag:ruby.yaml.org,2002:class'
end
It's a hack and I'll try to find a better solution but now it works for me.
(Rails 3.0.9, Ruby 1.9.2-p290, delayed_job 2.1.4)
https://groups.google.com/forum/?fromgroups=#!topic/delayed_job/_gvIcbXrOaE solved my handles_asynchronously error for class methods.
As per Brandon Keeper in the link above, the code is the following:
class ClassName
class << self
def foo
end
handle_asynchronously :foo
end
end
then use ClassName.delay.foo

Resources