Rails and rspec. uninitialized constant Errors - ruby-on-rails

I have a rails App. I have this file app/errors/status.rb
I try to pass the test but is not not working.
module Errors
class Status
def initialize status
#status = status
end
def default_message
"Error in the server status: #{status}"
end
private
attr_reader :status
end
end
And the test on spec/errors/status_spec.rb:
require 'rails_helper'
describe Errors::Status do
let(:status) { double 'status' }
subject { described_class.new status }
describe 'default_message' do
it 'returns the default message' do
expect(subject.call).to eq( "Error in the server status: #{status}")
end
end
end
And it keeps throwing this error:
/Users/gerardmorera/bet_play/spec/errors/status_spec.rb:3:in `<top (required)>': uninitialized constant Errors (NameError)
from /Users/gerardmorera/.rvm/rubies/ruby-2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1327:in `load'
from /Users/gerardmorera/.rvm/rubies/ruby-2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1327:in `block in load_spec_files'
from /Users/gerardmorera/.rvm/rubies/ruby-2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `each'
from /Users/gerardmorera/.rvm/rubies/ruby-2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `load_spec_files'
from /Users/gerardmorera/.rvm/rubies/ruby-2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:102:in `setup'
from /Users/gerardmorera/.rvm/rubies/ruby-2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:88:in `run'

That’s because of how ActiveSupport’s auto-loading works and the way in which Rails sets up the $LOAD_PATH. Autoload sees Errors::Status and expects to find it at errors/status somewhere via require, but it doesn’t because app/errors is in the $LOAD_PATH, so you would require your file with just require 'errors'.
You can fix this by moving app/errors/status.rb to a location ActiveSupport’s auto-loading expects (e.g. app/<something>/errors/status.rb). You can puts $LOAD_ATH to see all the possible locations (note that Rails will add all directories in app/ to the $LOAD_PATH).

Related

Rspec error uninitialized constant

I've got a recruitment task to do from a company, that concerns Rspec. After cloning their repo on which I have to work I immediately run on some errors. I am completely new in Rspec or testing in general actually, I would like to improve, but this error is something I can't find a solution to.
spec/validators/title_brackets_validator_spec.rb
require "rails_helper"
describe TitleBracketsValidator do
subject { Validatable.new(title: title) }
shared_examples "has valid title" do
it "should be valid" do
expect(subject).to be_valid
end
end
context "with curly brackets" do
let(:title) { "The Fellowship of the Ring {Peter Jackson}" }
it_behaves_like "has valid title"
end
[ more not important 'contexts'...]
end
class Validatable
include ActiveModel::Validations
validates_with TitleBracketsValidator
attr_accessor :title
def initialize(title:)
#title = title
end
end
and while running bundle exec rspec I get an error:
An error occurred while loading ./spec/validators/title_brackets_validator_spec.rb.
Failure/Error:
describe TitleBracketsValidator do
subject { Validatable.new(title: title) }
shared_examples "has valid title" do
it "should be valid" do
expect(subject).to be_valid
end
end
shared_examples "has invalid title" do
NameError:
uninitialized constant TitleBracketsValidator
# /var/lib/gems/2.3.0/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/active_support.rb:53:in `block in load_missing_constant'
# /var/lib/gems/2.3.0/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/active_support.rb:8:in `without_bootsnap_cache'
# /var/lib/gems/2.3.0/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/active_support.rb:53:in `rescue in load_missing_constant'
# /var/lib/gems/2.3.0/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/active_support.rb:43:in `load_missing_constant'
# ./spec/validators/title_brackets_validator_spec.rb:3:in `<top (required)>'
# /var/lib/gems/2.3.0/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:50:in `load'
# /var/lib/gems/2.3.0/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:50:in `load'
# ------------------
# --- Caused by: ---
# NameError:
# uninitialized constant TitleBracketsValidator
# /var/lib/gems/2.3.0/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/active_support.rb:43:in `load_missing_constant'
Finished in 0.00045 seconds (files took 1.57 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples
Coverage report generated for RSpec to /home/maciej/Templates/task_app/coverage. 0 / 108 LOC (0.0%) covered.
Read your assignment carefully - maybe one of your tasks is to add the file and implement the class TitleBracketsValidator so it passes all the provided tests?
I'd search for the class in your sources. If not there - maybe you should create one. If it's there - then it's a loading issue. require it in your specs and notify the recruiter that you fixed a possible bug (for extra points maybe)

Rspec not testing render, won't allow us to stub render action

Rails 5.0.0.1
Rspec 3.5.4
Ruby 2.3.1
We have been trying to provide test coverage for our rails application. We have a rescue in a private method that Rspec is not reaching.
Rspec:
it 'returns 200 after 404 from GET #edit error' do
allow(controller).to receive(:getpackages).and_return(URI::InvalidURIError)
expect(response.code).to eq(200) # => covers the 200
expect(response).to render_template('errors/5xx') # => doesn't read
end
Rails:
private
def set_package
#package = PackageServices.getpackage params[:id]
rescue URI::InvalidURIError
render 'errors/5xx'
end
Error message:
expecting <"errors/5xx"> but rendering with <[]>
./spec/controllers/packages_controller_spec.rb:139:in `block (3 levels) in <top (required)>'
-e:1:in `load'
-e:1:in `<main>'
We have tried to assert_template, tried to stub it using stub_template, installed a gem rails-controller-testing (not rspec), but we have run out of ideas and every google link is purple. Is this a bug in Rspec or are we going about it the wrong way?
I believe the stabbing was incorrect. Try the following code, it should work.
context 'URI is invalid' do
before do
allow(PackageServices).toreceive(:getpackage).and_raise(URI::InvalidURIError)
end
it 'returns 200 after 404 from GET #edit error' do
expect(response.code).to eq(200) # => covers the 200
expect(response).to render_template('errors/5xx') # => doesn't read
end
end

Uninitialized Constant Error: Factory Girl

Uninitialized constant Production::POverview (NameError)
from /Users/simon_zhu/.rvm/gems/ruby-2.1.0/gems/factory_girl-4.4.0/lib/factory_girl/syntax/default.rb:49:in `instance_eval'
from /Users/simon_zhu/.rvm/gems/ruby-2.1.0/gems/factory_girl-4.4.0/lib/factory_girl/syntax/default.rb:49:in `run'
from /Users/simon_zhu/.rvm/gems/ruby-2.1.0/gems/factory_girl-4.4.0/lib/factory_girl/syntax/default.rb:7:in `define'
from /Users/simon_zhu/Documents/original_version_carmel/carmel/spec/factories/poverview.rb:1:in `<top (required)>'
from /Users/simon_zhu/.rvm/gems/ruby-2.1.0/gems/factory_girl-4.4.0/lib/factory_girl/find_definitions.rb:20:in `block (2 levels) in find_definitions'
from /Users/simon_zhu/.rvm/gems/ruby-2.1.0/gems/factory_girl-4.4.0/lib/factory_girl/find_definitions.rb:19:in `each'
I have the following code:
poverview.rb (factory)
FactoryGirl.define do
factory :poverview, class: Production::POverview do
name "test"
status ["p", "d", "m"]
end
end
p_overview_controller_spec.rb (spec)
require 'spec_helper'
describe Production::POverviewController do
login_user
# GET Request to pod_info
describe 'GET pod_info' do
before(:each) do
#pods = Factory(:poverview)
get 'show', :format => :json, :name => #pods.name
get 'show', :format => :json, :status => #pods.status
end
it "should return the correct company when correct id is passed" do
body = JSON.parse(response.body)
for(pod in body[0])
if(pod['name'].eql? #pod.name)
#pod.status.should.include? pod['status']
end
end
end
end
This is my first time writing an integration test for Factory Girl and I have this uninitialized constant error.
Any Ideas how to solve this?
Also your test setup looks very odd. It definitely looks like you are confusing integration and controller test.
Here is a post to get you started. I recommend doing some more research on testing in rails.
https://semaphoreapp.com/blog/2014/02/11/rails-testing-antipatterns-controllers.html

Rspec unit test case getting failed

I have written some Rspec test cases in my spec/models/season_spec.rb file. They are as:-
require 'spec_helper'
describe Season do
it 'should not be without name' do
Season.new(:name=>nil,:number_of_weeks=>'3',:start_date=>'2012-02-07',:user_id=>'113').should_not be_valid
end
it 'should not be without number of weeks' do
Season.new(:name=>'Apurva',:number_of_weeks=>nil,:start_date=>'2012-02-07',:user_id=>'113').should_not be_valid
end
it 'should not be without start_date' do
Season.new(:name=>'Apurva',:number_of_weeks=>'3',:start_date=>nil,:user_id=>'113').should_not be_valid
end
it 'should not be without user_id' do
Season.new(:name=>'Apurva',:number_of_weeks=>'3',:start_date=>'2012-02-07',:user_id=>nil).should_not be_valid
end
it 'should be with valid attributes' do
Season.new(:name=>'Apurva',:number_of_weeks=>'3',:start_date=>'2012-02-07',:user_id=>'113').should be_valid
end
end
And in my model i have validated these fields as :-
class Season < ActiveRecord::Base
validates_presence_of :name,:number_of_weeks,:start_date,:user_id
end
But still the test cases are failed. And it is giving me following output:-
/usr/lib/ruby/gems/1.8/gems/bundler-1.0.21/lib/bundler/runtime.rb:138: warning: Insecure world writable dir /usr/lib/ruby/gems/1.8 in PATH, mode 040777
FFFFF
Failures:
/usr/lib/ruby/gems/1.8/gems/rspec-core-2.8.0/lib/rspec/core/formatters/base_text_formatter.rb:165:in `pending_fixed?': undefined method `pending_fixed?' for #<ActiveRecord::StatementInvalid:0xb6cd03c8> (NoMethodError)
from /usr/lib/ruby/gems/1.8/gems/rspec-core-2.8.0/lib/rspec/core/formatters/base_text_formatter.rb:19:in `dump_failures'
from /usr/lib/ruby/gems/1.8/bundler/gems/rails-27357a6965eb/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb:473:in `each_with_index'
from /usr/lib/ruby/gems/1.8/gems/rspec-core-2.8.0/lib/rspec/core/formatters/base_text_formatter.rb:17:in `each'
from /usr/lib/ruby/gems/1.8/gems/rspec-core-2.8.0/lib/rspec/core/formatters/base_text_formatter.rb:17:in `each_with_index'
from /usr/lib/ruby/gems/1.8/gems/rspec-core-2.8.0/lib/rspec/core/formatters/base_text_formatter.rb:17:in `dump_failures'
from /usr/lib/ruby/vendor_ruby/rspec/core/reporter.rb:75:in `send'
from /usr/lib/ruby/vendor_ruby/rspec/core/reporter.rb:75:in `notify'
from /usr/lib/ruby/vendor_ruby/rspec/core/reporter.rb:74:in `each'
from /usr/lib/ruby/vendor_ruby/rspec/core/reporter.rb:74:in `notify'
from /usr/lib/ruby/vendor_ruby/rspec/core/reporter.rb:23:in `conclude'
from /usr/lib/ruby/vendor_ruby/rspec/core/reporter.rb:14:in `report'
from /usr/lib/ruby/vendor_ruby/rspec/core/command_line.rb:24:in `run'
from /usr/lib/ruby/vendor_ruby/rspec/core/runner.rb:55:in `run_in_process'
from /usr/lib/ruby/vendor_ruby/rspec/core/runner.rb:46:in `run'
from /usr/lib/ruby/vendor_ruby/rspec/core/runner.rb:10:in `autorun'
from /usr/bin/rspec:4
First, there is a typo in the 'should not be without name' spec. Please check if this is just a typo while typing the quetion here, or in your code.
Second, these tests are pointless since that code is already tested here.

Undefined method 'action' running single RSpec file for a rails controller

I have a peculiar situation - an rspec file fails when run independently, but run okay when run as part of the entire suite.
Failure/Error: visit oauth_callback_path
NoMethodError:
undefined method `action' for MyController:Class
# <internal:prelude>:10:in `synchronize'
# ./spec/requests/login_spec.rb:xx:in `block (5 levels) in <top (required)>'
# ./spec/requests/login_spec.rb:xx:in `block (4 levels) in <top (required)>'
Simplified spec:
require 'spec_helper'
class MyController
def oauth_response
sign_in(
ENV['TEST_ACCESS_TOKEN'],
ENV['TEST_ACCESS_SECRET'])
redirect_to root_path
end
end
describe 'logging in' do
it 'login' do
visit oauth_callback_path
response.should be_success
end
end
I believe the problem is that MyController is not extending ApplicationController. That's why the method action is not defined for MyController.
The class MyController appears to be blocking Rails magic class loading. Either the test should explicitly require the controller, or the extension should be defined with MyController.class_eval

Resources