Using Rails URL helpers within a module - ruby-on-rails

I'm versioning my API with Versionist, so right now my routes file looks like
Rails.application.routes.draw do
api_version(:module => "V1", :path => {:value => "v1"}) do
resources :concepts, except: [:new, :edit]
end
end
And my controller is at app/controllers/v1/concepts_controller.rb.
And inside spec/controllers/v1/concepts_controller_spec.rb I have
...
describe "POST #create" do
context "with valid params" do
it "creates a new Concept" do
expect {
post :create, {:concept => valid_attributes}, valid_session
}.to change(Concept, :count).by(1)
end
...
end
end
...
describe "PUT #update" do
context "with valid params" do
let(:new_attributes) { {description: 'Sample description.'} }
it "updates the requested concept" do
concept = Concept.create! valid_attributes
put :update, {:id => concept.to_param, :concept => new_attributes}, valid_session
concept.reload
expect(concept.description).to eq('Sample description.')
end
...
The PUT #update tests work fine, but all the POST #create ones give me
1) V1::ConceptsController POST #create with valid params creates a new Concept
Failure/Error: post :create, {:concept => valid_attributes}, valid_session
NoMethodError:
undefined method `concept_url' for #<V1::ConceptsController:0x007fd6e6d8ed38>
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_dispatch/routing/polymorphic_routes.rb:268:in `handle_model_call'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_dispatch/routing/url_for.rb:167:in `url_for'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/metal/rendering.rb:95:in `_process_options'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/metal/renderers.rb:43:in `block in _render_to_body_with_renderer'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/metal/renderers.rb:41:in `_render_to_body_with_renderer'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/metal/renderers.rb:37:in `render_to_body'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/abstract_controller/rendering.rb:25:in `render'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/metal/rendering.rb:16:in `render'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
# /usr/local/bundle/gems/activesupport-4.2.2/lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
# /usr/local/bundle/gems/activesupport-4.2.2/lib/active_support/core_ext/benchmark.rb:12:in `ms'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/metal/instrumentation.rb:44:in `block in render'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
# /usr/local/bundle/gems/activerecord-4.2.2/lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/metal/instrumentation.rb:43:in `render'
# ./app/controllers/v1/concepts_controller.rb:24:in `create'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/abstract_controller/base.rb:198:in `process_action'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/metal/rendering.rb:10:in `process_action'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/abstract_controller/callbacks.rb:20:in `block in process_action'
# /usr/local/bundle/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:117:in `call'
# /usr/local/bundle/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:117:in `call'
# /usr/local/bundle/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
# /usr/local/bundle/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:505:in `call'
# /usr/local/bundle/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:505:in `call'
# /usr/local/bundle/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:92:in `_run_callbacks'
# /usr/local/bundle/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:776:in `_run_process_action_callbacks'
# /usr/local/bundle/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:81:in `run_callbacks'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/abstract_controller/callbacks.rb:19:in `process_action'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/metal/rescue.rb:29:in `process_action'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
# /usr/local/bundle/gems/activesupport-4.2.2/lib/active_support/notifications.rb:164:in `block in instrument'
# /usr/local/bundle/gems/activesupport-4.2.2/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
# /usr/local/bundle/gems/activesupport-4.2.2/lib/active_support/notifications.rb:164:in `instrument'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/metal/instrumentation.rb:30:in `process_action'
# /usr/local/bundle/gems/activerecord-4.2.2/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/abstract_controller/base.rb:137:in `process'
# /usr/local/bundle/gems/actionview-4.2.2/lib/action_view/rendering.rb:30:in `process'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/test_case.rb:632:in `process'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/test_case.rb:65:in `process'
# /usr/local/bundle/gems/actionpack-4.2.2/lib/action_controller/test_case.rb:514:in `post'
# ./spec/controllers/v1/concepts_controller_spec.rb:38:in `block (5 levels) in <top (required)>'
# ./spec/controllers/v1/concepts_controller_spec.rb:37:in `block (4 levels) in <top (required)>'
My rake routes:
Prefix Verb URI Pattern Controller#Action
v1_concepts GET /v1/concepts(.:format) v1/concepts#index
POST /v1/concepts(.:format) v1/concepts#create
v1_concept GET /v1/concepts/:id(.:format) v1/concepts#show
PATCH /v1/concepts/:id(.:format) v1/concepts#update
PUT /v1/concepts/:id(.:format) v1/concepts#update
DELETE /v1/concepts/:id(.:format) v1/concepts#destroy
How do I get it to use v1_concept_url instead? And why is it even calling concept_url when it isn't even a GET request?'
EDIT My controller:
class V1::ConceptsController < V1::BaseController
before_action :set_concept, only: [:show, :update, :destroy]
# GET /concepts
# GET /concepts.json
def index
#concepts = Concept.all
render json: #concepts
end
# GET /concepts/1
# GET /concepts/1.json
def show
render json: #concept
end
# POST /concepts
# POST /concepts.json
def create
#concept = Concept.new(concept_params)
if #concept.save
render json: #concept, status: :created, location: #concept
else
render json: #concept.errors, status: :unprocessable_entity
end
end
...

Apparently the location header is the culprit. Passing in the instance variable #concept causes Rails to invoke the concept_url helper.
You can override this by invoking the helper yourself:
location: v1_concept_url(#concept)

Related

Rails Integration testing with rspec and fixture_file_upload

I recently ran into an issue with an integration test using rspec.
RSpec.describe "Images", type: :request do
before(:each) do
#user = User.create(username: "sam", password: "password")
#tag = #user.tags.create(name: "outdoors")
post login_path, params: { username: "sam", password: "password" }
expect(session[:user_id]).to eql #user.id
#file = fixture_file_upload("/dingo.jpeg", "image/jpeg")
end
it "can show images" do
#image = #user.images.create(caption: "hello", image_file: #image)
get user_image_path(#user, #image)
expect(response).to be_success
end
end
The before each creates a valid user, tag and logs the user in. Then it loads an image file.
The test it's self creates a new image for the #user and assigns it a value for caption and the loaded file for its :image_file.
The Image model uses has_one_attached :image_file to associate an uploaded image with an image record
class Image < ApplicationRecord
belongs_to :user
has_and_belongs_to_many :tags
has_one_attached :image_file
end
the controller and action as follows:
class ImageController < ApplicationController
before_action :unauthorized_redirect
def show
#user = current_user
#image = Image.find(params[:id])
#tags = #user.tags.all
authorize #image, :show?
end
private
def image_params
params.require(:image).permit(:caption, :image_file, tag_ids: [])
end
end
When I run the test I get an error:
Failures:
1) Images can show images
Failure/Error: <%= image_tag #image.image_file, class: "img-fluid show-image" %>
ActionView::Template::Error:
Can't resolve image into URL: undefined method `persisted?' for nil:NilClass
# ./app/views/image/show.html.erb:7:in `_app_views_image_show_html_erb__3727703739275822003_20580'
# <internal:kernel>:90:in `tap'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/actiontext-6.1.4.1/lib/action_text/rendering.rb:20:in `with_renderer'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/actiontext-6.1.4.1/lib/action_text/engine.rb:59:in `block (4 levels) in <class:Engine>'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/tempfile_reaper.rb:15:in `call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/etag.rb:27:in `call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/conditional_get.rb:27:in `call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/head.rb:12:in `call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/session/abstract/id.rb:266:in `context'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/session/abstract/id.rb:260:in `call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/railties-6.1.4.1/lib/rails/rack/logger.rb:37:in `call_app'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/railties-6.1.4.1/lib/rails/rack/logger.rb:26:in `block in call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/railties-6.1.4.1/lib/rails/rack/logger.rb:26:in `call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/method_override.rb:24:in `call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/runtime.rb:22:in `call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/sendfile.rb:110:in `call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/railties-6.1.4.1/lib/rails/engine.rb:539:in `call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-test-1.1.0/lib/rack/mock_session.rb:29:in `request'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-test-1.1.0/lib/rack/test.rb:266:in `process_request'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-test-1.1.0/lib/rack/test.rb:119:in `request'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rails-controller-testing-1.0.5/lib/rails/controller/testing/integration.rb:16:in `block (2 levels) in <module:Integration>'
# ./spec/requests/images_spec.rb:38:in `block (2 levels) in <main>'
# ------------------
# --- Caused by: ---
# NoMethodError:
# undefined method `persisted?' for nil:NilClass
# ./app/views/image/show.html.erb:7:in `_app_views_image_show_html_erb__3727703739275822003_20580'
Finished in 0.79372 seconds (files took 0.46552 seconds to load)
30 examples, 1 failure
Failed examples:
rspec ./spec/requests/images_spec.rb:36 # Images can show images
I have debugged this issue with breakpoints everywhere and I just don't understand why it's not working in test when it works in production. I hope I've provided enough information this bug has been driving me crazy for the past two days.
Thank you!
Versions:
rails: 6.1.4.1
rspec: 5.0.2
Changed one line and fixed it
it "can show images" do
#image = #user.images.create(caption: "hello", image_file: #image)
get user_image_path(#user, #image)
*expect(response).to be_success ----REMOVE*
expect(response.successful?).to eql true
end

Ruby on Rails -- NilClass error on Faraday GET request

Using Rails 5.1.4, Ruby 2.4
I am attempting to connect to an API using Faraday which I've done before without issue, but I'm getting the following error when I run RSpec:
SearchController GET #index returns http success
Failure/Error: response = #conn.get("/congress/v1/members/house/#{state}/current.json")
NoMethodError:
undefined method `get' for nil:NilClass
Did you mean? gem
# ./app/controllers/search_controller.rb:9:in `block in index'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/faraday-0.13.1/lib/faraday/connection.rb:88:in `initialize'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/faraday-0.13.1/lib/faraday.rb:67:in `new'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/faraday-0.13.1/lib/faraday.rb:67:in `new'
# ./app/controllers/search_controller.rb:5:in `index'
# ./spec/controllers/search_controller_spec.rb:7:in `block (3 levels) in <top (required)>'
2) user can search for house members user submits a valid state name
Failure/Error: response = #conn.get("/congress/v1/members/house/#{state}/current.json")
NoMethodError:
undefined method `get' for nil:NilClass
Did you mean? gem
# ./app/controllers/search_controller.rb:9:in `block in index'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/faraday-0.13.1/lib/faraday/connection.rb:88:in `initialize'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/faraday-0.13.1/lib/faraday.rb:67:in `new'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/faraday-0.13.1/lib/faraday.rb:67:in `new'
# ./app/controllers/search_controller.rb:5:in `index'
# /Users/user/.rvm/gems/ruby-2.4.0#global/gems/rack-2.0.3/lib/rack/etag.rb:25:in `call'
# /Users/user/.rvm/gems/ruby-2.4.0#global/gems/rack-2.0.3/lib/rack/conditional_get.rb:25:in `call'
# /Users/user/.rvm/gems/ruby-2.4.0#global/gems/rack-2.0.3/lib/rack/head.rb:12:in `call'
# /Users/user/.rvm/gems/ruby-2.4.0#global/gems/rack-2.0.3/lib/rack/session/abstract/id.rb:232:in `context'
# /Users/user/.rvm/gems/ruby-2.4.0#global/gems/rack-2.0.3/lib/rack/session/abstract/id.rb:226:in `call'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/railties-5.1.4/lib/rails/rack/logger.rb:36:in `call_app'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/railties-5.1.4/lib/rails/rack/logger.rb:24:in `block in call'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/railties-5.1.4/lib/rails/rack/logger.rb:24:in `call'
# /Users/user/.rvm/gems/ruby-2.4.0#global/gems/rack-2.0.3/lib/rack/method_override.rb:22:in `call'
# /Users/user/.rvm/gems/ruby-2.4.0#global/gems/rack-2.0.3/lib/rack/runtime.rb:22:in `call'
# /Users/user/.rvm/gems/ruby-2.4.0#global/gems/rack-2.0.3/lib/rack/sendfile.rb:111:in `call'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/railties-5.1.4/lib/rails/engine.rb:522:in `call'
# /Users/user/.rvm/gems/ruby-2.4.0#global/gems/rack-2.0.3/lib/rack/urlmap.rb:68:in `block in call'
# /Users/user/.rvm/gems/ruby-2.4.0#global/gems/rack-2.0.3/lib/rack/urlmap.rb:53:in `each'
# /Users/user/.rvm/gems/ruby-2.4.0#global/gems/rack-2.0.3/lib/rack/urlmap.rb:53:in `call'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/rack-test-0.7.0/lib/rack/mock_session.rb:30:in `request'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/rack-test-0.7.0/lib/rack/test.rb:249:in `process_request'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/rack-test-0.7.0/lib/rack/test.rb:59:in `get'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/capybara-2.15.1/lib/capybara/rack_test/browser.rb:69:in `process'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/capybara-2.15.1/lib/capybara/rack_test/browser.rb:41:in `process_and_follow_redirects'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/capybara-2.15.1/lib/capybara/rack_test/browser.rb:32:in `submit'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/capybara-2.15.1/lib/capybara/rack_test/form.rb:78:in `submit'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/capybara-2.15.1/lib/capybara/rack_test/node.rb:64:in `click'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/capybara-2.15.1/lib/capybara/node/element.rb:143:in `block in click'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/capybara-2.15.1/lib/capybara/node/base.rb:85:in `synchronize'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/capybara-2.15.1/lib/capybara/node/element.rb:143:in `click'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/capybara-2.15.1/lib/capybara/node/actions.rb:25:in `click_link_or_button'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/capybara-2.15.1/lib/capybara/session.rb:776:in `block (2 levels) in <class:Session>'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/capybara-2.15.1/lib/capybara/dsl.rb:50:in `block (2 levels) in <module:DSL>'
# ./spec/features/user_can_search_by_state_spec.rb:11:in `block (2 levels) in <top (required)>'
Here is my Search controller, where the logic resides until I can fix this error and extract it elsewhere:
class SearchController < ApplicationController
def index
state = params[:state]
#conn = Faraday.new(url: "https://api.propublica.org") do |faraday|
faraday.headers["X-API-KEY"] = "<$MY-API-KEY>"
faraday.adapter Faraday.default_adapter
response = #conn.get("/congress/v1/members/house/#{state}/current.json")
results = JSON.parse(response.body, symbolize_names: true)[:results]
#members = results.map do |result|
Member.new(result)
end
end
end
end
And to be thorough, here is my Member model:
class Member
attr_reader :name,
:role,
:party,
:district,
:seniority
def initialize(attributes = {})
#name = attributes[:name]
#role = attributes[:role]
#party = attributes[:party]
#district = attributes[:district]
#seniority = attributes[:seniority]
end
end
#conn is not being properly defined, so when it tries to call #conn.get it's really calling nil.get which raises undefined method get for nil. Ensure you have defined #conn before you try to reference it. I have cleaned up your example to show the issue:
class SearchController < ApplicationController
def index
state = params[:state]
#conn = Faraday.new(url: "https://api.propublica.org") do |faraday|
faraday.headers["X-API-KEY"] = "<$MY-API-KEY>"
faraday.adapter Faraday.default_adapter
# you are trying to reference #conn here
response = #conn.get("/congress/v1/members/house/#{state}/current.json")
results = JSON.parse(response.body, symbolize_names: true)[:results]
#members = results.map do |result|
Member.new(result)
end
end # but #conn is not defined until you get here
end
end
If you refactor your code, it should look something more like this:
class SearchController < ApplicationController
def index
state = params[:state]
# fully define your #conn object
#conn = Faraday.new(url: "https://api.propublica.org") do |faraday|
faraday.headers["X-API-KEY"] = "<$MY-API-KEY>"
faraday.adapter Faraday.default_adapter
end
# now you can reference it
response = #conn.get("/congress/v1/members/house/#{state}/current.json")
results = JSON.parse(response.body, symbolize_names: true)[:results]
#members = results.map do |result|
Member.new(result)
end
end
end

Rspec: Testing registarion routing in rails with devise

I'm Trying to build a simple Blog using rails 4 and For authentication I'm using Devise.
The blog is simple and it will only have one registered user which is the admin. so the registration page should be available when there are no users in the database else it will redirect to the home page.
I'm trying to learn testing in the process. So I started testing the RegistrationContrroler However it doesn't seem to pass.
class RegistrationsController < Devise::RegistrationsController
before_action :one_user_registered?, only: [:new, :create]
protected
def one_user_registered?
if ((User.count == 1) & (user_signed_in?))
redirect_to root_path
elsif User.count == 1
redirect_to new_user_session_path
end
end
end
Specs:
RSpec.describe RegistrationsController, type: :controller do
context "Admin already registred" do
it "cannot create new user" do
#user_attr = FactoryGirl.attributes_for(:user)
# Create Admin
User.create!(#user_attr)
#request.env["devise.mapping"] = Devise.mappings[:user]
post :create, user: #user_attr
User.count should eq_to(1)
end
end
end
spec/factories.rb :
FactoryGirl.define do
factory :user do
sequence(:email) { |n| "user#{n}#example.com" }
password "secure"
end
factory :post do
sequence(:id){ |n| n }
sequence(:title) { |n| "Post title #{n}" }
body "Post body"
sequence(:slug) { |n| "Post title #{n}" }
end
end
routes.rb
Rails.application.routes.draw do
devise_for :users, controllers: { registrations: "registrations" }
authenticated :user do
resources :posts
end
unauthenticated :user do
resources :posts, only: [:show, :index]
end
root 'posts#index'
end
Stacktrace :
F
Failures:
1) RegistrationsController Admin already registred cannot create new user
Failure/Error: User.count should eq_to(1)
expected: 1
got: #<RegistrationsController:0x00000003a413b8 #_action_has_layout=true, #_routes=nil, #_headers={"Conten...enticatable]>, #req touest_format=:html, #marked_for_same_origin_verification=false, #current_user=nil>
(compared using ==)
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-support-3.5.0/lib/rspec/support.rb:87:in `block in <module:Support>'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-support-3.5.0/lib/rspec/support.rb:96:in `notify_failure'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-expectations-3.5.0/lib/rspec/expectations/fail_with.rb:27:in `fail_with'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-expectations-3.5.0/lib/rspec/expectations/handler.rb:38:in `handle_failure'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-expectations-3.5.0/lib/rspec/expectations/handler.rb:50:in `block in handle_matcher'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-expectations-3.5.0/lib/rspec/expectations/handler.rb:27:in `with_matcher'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-expectations-3.5.0/lib/rspec/expectations/handler.rb:48:in `handle_matcher'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/memoized_helpers.rb:81:in `should'
# ./spec/controllers/registrations_controller_spec.rb:13:in `block (3 levels) in <top (required)>'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:252:in `instance_exec'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:252:in `block in run'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:494:in `block in with_around_and_singleton_context_hooks'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:451:in `block in with_around_example_hooks'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/hooks.rb:471:in `block in run'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/hooks.rb:611:in `block in run_around_example_hooks_for'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:336:in `call'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-rails-3.5.1/lib/rspec/rails/example/controller_example_group.rb:191:in `block (2 levels) in <module:ControllerExampleGroup>'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:441:in `instance_exec'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:441:in `instance_exec'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/hooks.rb:382:in `execute_with'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/hooks.rb:613:in `block (2 levels) in run_around_example_hooks_for'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:336:in `call'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-rails-3.5.1/lib/rspec/rails/adapters.rb:127:in `block (2 levels) in <module:MinitestLifecycleAdapter>'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:441:in `instance_exec'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:441:in `instance_exec'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/hooks.rb:382:in `execute_with'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/hooks.rb:613:in `block (2 levels) in run_around_example_hooks_for'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:336:in `call'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/hooks.rb:614:in `run_around_example_hooks_for'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/hooks.rb:471:in `run'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:451:in `with_around_example_hooks'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:494:in `with_around_and_singleton_context_hooks'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example.rb:249:in `run'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example_group.rb:627:in `block in run_examples'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example_group.rb:623:in `map'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example_group.rb:623:in `run_examples'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example_group.rb:589:in `run'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example_group.rb:590:in `block in run'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example_group.rb:590:in `map'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/example_group.rb:590:in `run'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/runner.rb:113:in `block (3 levels) in run_specs'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/runner.rb:113:in `map'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/runner.rb:113:in `block (2 levels) in run_specs'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/configuration.rb:1836:in `with_suite_hooks'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/runner.rb:112:in `block in run_specs'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/reporter.rb:77:in `report'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/runner.rb:111:in `run_specs'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/runner.rb:87:in `run'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/runner.rb:71:in `run'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/lib/rspec/core/runner.rb:45:in `invoke'
# /home/chaker/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.1/exe/rspec:4:in `<top (required)>'
# /home/chaker/.rvm/gems/ruby-2.3.1/bin/rspec:23:in `load'
# /home/chaker/.rvm/gems/ruby-2.3.1/bin/rspec:23:in `<main>'
# /home/chaker/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `eval'
# /home/chaker/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `<main>'
Finished in 0.08188 seconds (files took 1.95 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/controllers/registrations_controller_spec.rb:7 # RegistrationsController Admin already registred cannot create new user
You're missing a dot!
User.count.should eq_to(1)
Adding a should method is the way rspec patches all objects to support the should comparisons.
Preferable would be...
expect(User.count).to eq(1)
The error showed that the should by itself was received by the RegistrationsController, not by the User.count... and of course the RegistrationsController doesn't equal 1.

RSPEC test fails to POST a "create" action for nested resource

I have a resource called "parameters" that is nested under "tests" in my API. There relation is as follows: "A test has many parameters". I've structured my routes as follows
tests/:test_id/parameters/:parameter_id
and my rake routes gives:
test_parameters GET /tests/:test_id/parameters(.:format) parameters#index {:format=>:json}
POST /tests/:test_id/parameters(.:format) parameters#create {:format=>:json}
new_test_parameter GET /tests/:test_id/parameters/new(.:format) parameters#new {:format=>:json}
edit_test_parameter GET /tests/:test_id/parameters/:id/edit(.:format) parameters#edit {:format=>:json}
test_parameter GET /tests/:test_id/parameters/:id(.:format) parameters#show {:format=>:json}
PATCH /tests/:test_id/parameters/:id(.:format) parameters#update {:format=>:json}
PUT /tests/:test_id/parameters/:id(.:format) parameters#update {:format=>:json}
DELETE /tests/:test_id/parameters/:id(.:format) parameters#destroy {:format=>:json}
I'm writing simple rspec tests to test all the typical actions of my API. Right now, my POST/CREATE is giving me a very puzzling error.
Failures:
1) ParametersController POST create a new parameter with valid attributes successfully with name and times
Failure/Error: post :create, :test_id => #test.id, parameter: parameter.attributes, format: :json
NoMethodError:
undefined method `parameter_url' for #<ParametersController:0x000001072b4280>
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_dispatch/routing/polymorphic_routes.rb:220:in `polymorphic_method'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_dispatch/routing/polymorphic_routes.rb:114:in `polymorphic_url'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_dispatch/routing/url_for.rb:163:in `url_for'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/rendering.rb:95:in `_process_options'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/streaming.rb:200:in `_process_options'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/renderers.rb:43:in `block in _render_to_body_with_renderer'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/renderers.rb:41:in `_render_to_body_with_renderer'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/renderers.rb:37:in `render_to_body'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/abstract_controller/rendering.rb:25:in `render'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/rendering.rb:16:in `render'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activesupport-4.2.2/lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activesupport-4.2.2/lib/active_support/core_ext/benchmark.rb:12:in `ms'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/instrumentation.rb:44:in `block in render'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activerecord-4.2.2/lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/instrumentation.rb:43:in `render'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/responders-2.1.0/lib/action_controller/responder.rb:258:in `display'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/responders-2.1.0/lib/action_controller/responder.rb:214:in `api_behavior'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/responders-2.1.0/lib/action_controller/responder.rb:191:in `rescue in to_format'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/responders-2.1.0/lib/action_controller/responder.rb:185:in `to_format'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/responders-2.1.0/lib/action_controller/responder.rb:163:in `respond'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/responders-2.1.0/lib/action_controller/responder.rb:156:in `call'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/responders-2.1.0/lib/action_controller/respond_with.rb:203:in `respond_with'
# ./app/controllers/parameters_controller.rb:13:in `create'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/abstract_controller/base.rb:198:in `process_action'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/rendering.rb:10:in `process_action'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/abstract_controller/callbacks.rb:20:in `block in process_action'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:117:in `call'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:117:in `call'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:505:in `call'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:505:in `call'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:92:in `_run_callbacks'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:776:in `_run_process_action_callbacks'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activesupport-4.2.2/lib/active_support/callbacks.rb:81:in `run_callbacks'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/abstract_controller/callbacks.rb:19:in `process_action'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/rescue.rb:29:in `process_action'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activesupport-4.2.2/lib/active_support/notifications.rb:164:in `block in instrument'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activesupport-4.2.2/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activesupport-4.2.2/lib/active_support/notifications.rb:164:in `instrument'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/instrumentation.rb:30:in `process_action'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/activerecord-4.2.2/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/abstract_controller/base.rb:137:in `process'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionview-4.2.2/lib/action_view/rendering.rb:30:in `process'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/test_case.rb:632:in `process'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/test_case.rb:65:in `process'
# /Users/naseem.alnaji/.rvm/gems/ruby-2.1.1/gems/actionpack-4.2.2/lib/action_controller/test_case.rb:514:in `post'
# ./spec/controllers/parameters_controller_spec.rb:34:in `block (5 levels) in <top (required)>'
# ./spec/controllers/parameters_controller_spec.rb:33:in `block (4 levels) in <top (required)>'
I've tried changing the formats in how I'm submitting the POST but that hadn't changed anything. For now, my rspec test looks like this:
"spec/controllers/parameters_controller_spec.rb"
require 'spec_helper'
describe ParametersController do
before :each do
#test = FactoryGirl.create(:test, id: 1, name: 'Eccentric Tech')
#test.save
end
# ...
describe 'POST create a new parameter' do
context 'with valid attributes' do
it 'successfully with name and times' do
parameter = FactoryGirl.build(:parameter)
expect {
post :create, :test_id => #test.id, parameter: parameter.attributes, format: :json
}.to change(Parameter, :count).by(1)
end
end
context 'with invalid attributes' do
it 'fails without a name' do
parameter = FactoryGirl.build(:no_name_parameter)
expect {
post :create, :test_id => #test.id, parameter: parameter.attributes, format: :json
}.to change(Parameter, :count).by(0)
end
it 'fails without belonging to a test' do
parameter = FactoryGirl.build(:no_test_parameter)
expect {
post :create, :test_id => #test.id, parameter: parameter.attributes, format: :json
}.to change(Parameter, :count).by(0)
end
end
end
# ...
end
My Factory:
"spec/factories/parameters.rb"
require 'faker'
FactoryGirl.define do
factory :parameter do |f|
f.name { "#{Faker::Hacker.noun}" }
f.value { Faker::Number.number(2) }
f.test_id { 1 }
end
factory :no_name_parameter, parent: :parameter do |f|
f.name nil
f.value { Faker::Number.number(2) }
f.test_id { 1 }
end
factory :no_test_parameter, parent: :parameter do |f|
f.name { "#{Faker::Hacker.noun}" }
f.value { Faker::Number.number(2) }
f.test_id nil
end
end
And finally, my controller:
class ParametersController < ApplicationController
respond_to :json
def index
respond_with Parameter.all
end
def show
respond_with Parameter.find(params[:id])
end
def create
respond_with Parameter.create(parameter_params)
end
def update
respond_with Parameter.update(params[:id], parameter_params)
end
def destroy
respond_with Parameter.destroy(params[:id])
end
private
def parameter_params
params.require(:parameter).permit(:name, :value, :test_id)
end
end
routes.rb:
Rails.application.routes.draw do
scope :defaults => { :format => :json } do
resources :services do
resources :tests
end
resources :tests do
resources :parameters
resources :singular_results
resources :dataset_results
end
end
end
I solved this myself but I have nooooo idea why it works. If someone could explain it please do :)
All I did was make this small change to my controller:
def create
respond_with Parameter.create(parameter_params), location: nil
end
Found a good explanation: Rails: NoMethodError (Undefined method _url for _controller. I can't seem to respond_with json properly after my create. Why?

Tests for Devise user that has polymorphic profiles

My application has an User model that belongs_to :profile, :polymorphic => true. This profile can be Student, Manager, Enterprise or Employee and it needs to be approved, by its administrator, to log on the system. I used this approval approach.
I'm having a little troubles with the test part. It always outputs:
1) Error:
test_should_get_dashboard(ManagersControllerTest):
NoMethodError: undefined method `approved' for nil:NilClass
Probably relevant sources:
class User < ActiveRecord::Base
belongs_to :profile, :polymorphic => true
attr_accessible :approved
end
class ManagersControllerTest < ActionController::TestCase
include Devise::TestHelpers
fixtures :managers
fixtures :users
def setup
manager = Manager.create(:name => 'Manager', :cpf => '537.846.919-86')
#user = User.new(:email => 'user#user.com', :password => 'user#user.com')
#user.approved = true
#user.profile = manager
#user.save!
end
test "should get dashboard" do
sign_in #user
get :dashboard
assert_response :redirect
assert_redirected_to managers_root_path
end
end
class ManagersController < ApplicationController
def dashboard
p = Proc.new { |e| [e.name, e.id] if e.user.approved }
app_en = Enterprise.all.map(&p).compact.count
napp_en = Enterprise.count - app_en
app_em = Employee.all.map(&p).compact.count
napp_em = Employee.count - app_em
#app = [ app_en, app_em, 0 ]
#napp = [ napp_en, napp_em, 0 ]
#total = [ app_en + napp_en, app_em + napp_em, Student.count ]
tags_count = Activity.tag_counts.map(&:count)
#tags = Hash[Activity.tag_counts.map(&:name).zip(tags_count)]
#user = current_user
end
end
As I'm having troubles with the fixtures written, I'm ignoring them, but I would be very thankful if someone show a solution with fixtures. Even this way I'm getting stuck in this error. What should I do?
The Stack
/home/waldyr/Dropbox/Workspace/Rails/bancodevagas/app/controllers/managers_controller.rb:7:in `block in dashboard'
/home/waldyr/Dropbox/Workspace/Rails/bancodevagas/app/controllers/managers_controller.rb:8:in `map'
/home/waldyr/Dropbox/Workspace/Rails/bancodevagas/app/controllers/managers_controller.rb:8:in `dashboard'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/abstract_controller/base.rb:167:in `process_action'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/action_controller/metal/rendering.rb:10:in `process_action'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/abstract_controller/callbacks.rb:18:in `block in process_action'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:425:in `_run__2212830968385571096__process_action__4077155092621945467__callbacks'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:405:in `__run_callback'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:81:in `run_callbacks'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/abstract_controller/callbacks.rb:17:in `process_action'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/action_controller/metal/rescue.rb:29:in `process_action'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/activesupport-3.2.6/lib/active_support/notifications.rb:123:in `block in instrument'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/activesupport-3.2.6/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/activesupport-3.2.6/lib/active_support/notifications.rb:123:in `instrument'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/action_controller/metal/instrumentation.rb:29:in `process_action'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/action_controller/metal/params_wrapper.rb:206:in `process_action'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.6/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/abstract_controller/base.rb:121:in `process'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/abstract_controller/rendering.rb:45:in `process'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/action_controller/metal/testing.rb:17:in `process_with_new_base_test'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/action_controller/test_case.rb:469:in `process'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/action_controller/test_case.rb:49:in `process'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/devise-2.1.2/lib/devise/test_helpers.rb:19:in `block in process'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/devise-2.1.2/lib/devise/test_helpers.rb:71:in `catch'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/devise-2.1.2/lib/devise/test_helpers.rb:71:in `_catch_warden'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/devise-2.1.2/lib/devise/test_helpers.rb:19:in `process'
/home/waldyr/.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.2.6/lib/action_controller/test_case.rb:386:in `get'
/home/waldyr/Dropbox/Workspace/Rails/bancodevagas/test/functional/manager_controller_test.rb:24:in `block in <class:ManagersControllerTest>'
The answer appears to be that not all the Enterprise or Employee models actually have an associated user. When using map to e.user.approved, the controller tries to pass .approved to user which can be nil in this case.
One simple solution is to change your Proc to be nil safe:
p = Proc.new { |e| [e.name, e.id] if e.user && e.user.approved }

Resources