Rspec error uninitialized constant - ruby-on-rails

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)

Related

Rails and rspec. uninitialized constant Errors

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).

rspec Ruby on Rails uninitialized constant RecipesController::Recipes

hello i'm doing some test of my application with Rspec (this is my very first time i'm using it)
this is my test file located in spec/controllers/recipes_controller_spec.rb:
require 'spec_helper'
describe RecipesController do
render_views
describe "index" do
before do
Recipe.create!(name: 'Baked Potato w/ Cheese')
Recipe.create!(name: 'Garlic Mashed Potatoes')
Recipe.create!(name: 'Potatoes Au Gratin')
Recipe.create!(name: 'Baked Brussel Sprouts')
xhr :get, :index, format: :json, keywords: keywords
end
subject(:results) { JSON.parse(response.body) }
def extract_name
->(object) { object["name"] }
end
context "when the search finds results" do
let(:keywords) { 'baked' }
it 'should 200' do
expect(response.status).to eq(200)
end
it 'should return two results' do
expect(results.size).to eq(2)
end
it "should include 'Baked Potato w/ Cheese'" do
expect(results.map(&extract_name)).to include('Baked Potato w/ Cheese')
end
it "should include 'Baked Brussel Sprouts'" do
expect(results.map(&extract_name)).to include('Baked Brussel Sprouts')
end
end
context "when the search doesn't find results" do
let(:keywords) { 'foo' }
it 'should return no results' do
expect(results.size).to eq(0)
end
end
end
end
when i try to execute it by the command:
bundle exec rspec spec/controllers/recipes_controller_spec.rb
i fail all my tests with this error:
Failure/Error: xhr :get, :index, format: :json, keywords: keywords
NameError:
uninitialized constant RecipesController::Recipes
# ./app/controllers/recipes_controller.rb:4:in `index'
# ./spec/controllers/recipes_controller_spec.rb:12:in `block (3 levels) in <top (required)>'
i've tried to look all my code but i haven't find out the error
NameError: uninitialized constant RecipesController::Recipes
means you used Recipes instead of Recipe somewhere (line 4 in index) in controller, and since your model is called Recipe (singular), you're getting NameError exception.

Rails test spec will not build my user model

There are so many moving parts to Rails testing that it's difficult to debug a failing test sometimes.
As a case in point, I am attempting to use zeus, guard, rspec, capybara, Poltergeist, PhantomJS, and FactoryGirl to simply build a User factory.
I am met with the following error message:
spec/models/user.rb
require 'spec_helper'
describe User do
describe 'Factories' do
it { expect(build :user).to be_valid }
end
end
spec/factories/users.rb
FactoryGirl.define do
factory :user do
sequence(:email) { |n| Forgery(:internet).email_address }
sequence(:company) { |n| Forgery(:name).company_name }
password "password"
password_confirmation "password"
end
end
Now I have a request spec that fails on the following line, which is the setup at the beginning:
let!(:user) { login_user }
The failure is this:
Failure/Error: let!(:user) { login_user }
NoMethodError:
undefined method `last_logged_in_at=' for #<User:0x007fe60aca95e8>
# ./app/models/user.rb:136:in `set_login_time'
# ./app/controllers/sessions_controller.rb:24:in `create'
# ./spec/support/login_macros.rb:7:in `login_user'
# ./spec/requests/app_integration_spec.rb:5:in `block (2 levels) in <top (required)>'
# ./custom_plan.rb:12:in `spec'
# -e:1:in `<main>'
So the problem appears to be that since User is not a real object, there is no such attribute as last_logged_in_at. So I should have to mock that somewhere. I tried putting this inside the factory code:
before(:build) do |u|
u.stub!(:read_attribute).with(:last_logged_in_at).and_return(Time.now)
end
The error remains the same. The test simply can not find this attribute. Keep in mind this code works perfectly in the browser. I am out of my league. Where am I going wrong?
Is your test db schema up-to-date?
rake db:test:prepare

Rails Tutorial mass assignment security exception

I've been working through the Ruby on Rails Tutorial. I've run into a problem getting a test to pass that checks for the a mass assignment security exception to be thrown. I'm not sure why I'm getting this test failure, or how to fix it.
rspec:
describe "accessible attributes" do
it "should not allow access to user_id" do
expect do
Micropost.new(user_id: user.id)
end.should raise_error(ActiveModel::MassAssignmentSecurity::Error)
end
end
Failures:
1) Micropost accessible attributes should not allow access to user_id
Failure/Error: expect { Micropost.new(user_id: user.id) }.should raise_error(ActiveModel::MassAssignmentSecurity::Error)
expected ActiveModel::MassAssignmentSecurity::Error, got #<NoMethodError: undefined method `call' for #<RSpec::Expectations::ExpectationTarget:0x8af2bb8>>
# ./spec/models/micropost_spec.rb:23:in `block (3 levels) in <top (required)>
Try using to instead of should for your expect raise_error matcher.
describe "accessible attributes" do
it "should not allow access to user_id" do
expect do
Micropost.new(user_id: user.id)
end.to raise_error(ActiveModel::MassAssignmentSecurity::Error)
end
end

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.

Resources