`alias_method': undefined method `to_json' for class `Object' (NameError) - ruby-on-rails

I want running my test through Rspec but getting errors full trace here
one of them is 'alias_method': undefined methodto_json' for class `Object' (NameError)' and i even dont know from where it comes
require 'rubygems'
require 'spork'
Spork.prefork do
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'capybara/rspec'
require 'rspec/rails'
require 'database_cleaner'
require 'factory_girl'
Dir[Rails.root + 'spec/support/**/*.rb'].map &method(:require)
RSpec.configure do |config|
config.include Capybara::DSL
config.include FactoryGirl::Syntax::Methods
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.mock_with :rspec
config.order = :random
config.use_transactional_fixtures = false
config.use_instantiated_fixtures = true
config.global_fixtures = :all
config.infer_spec_type_from_file_location!
config.raise_errors_for_deprecations!
config.output_stream = 'rspec.xml'
config.formatter = RspecJunitFormatter
config.before do
DatabaseCleaner.start
ActionMailer::Base.deliveries.clear
end
config.after do
DatabaseCleaner.clean
end
end
end
chunk of gemfile
group :development, :test do
gem 'better_errors'
gem 'binding_of_caller'
gem 'capybara'
gem 'database_cleaner'
gem 'factory_girl_rails', '~> 4.0'
gem 'faker', git: 'https://github.com/stympy/faker.git'
gem 'pry'
gem 'rspec-rails', '~>3.0.0'
gem 'selenium-webdriver'
gem 'spork', :github => 'sporkrb/spork'
gem 'spork-rails', :github => 'sporkrb/spork-rails'
gem 'rspec_junit_formatter', :git => 'https://github.com/sj26/rspec_junit_formatter.git'
gem 'rspec-core'
gem 'rubocop', require: false
end
Any suggestions how to fix it?
Ruby 2.2.0
Rails 4.1.9

Related

Why Rspec is cleaning database on 'bundle exec rake spec'?

I don't understand strange thing.
I have table in my Postgresql database with 200 items
psql: \c app_test; SELECT COUNT(*) FROM items -->200
After installing rspec rspec-rails gem in rails and executing
RAILS_ENV=test bundle exec rake spec
...
psql: \c app_test; SELECT COUNT(*) FROM items -->0
Seems to be it is cleaning (purge) DB. Why?
Gemfile
group :development, :test do
gem 'pry-nav', '>= 0.2.3'
gem 'pry-rails', '>= 0.2.2'
gem 'letter_opener'
gem 'factory_girl_rails', '4.2.1'
gem 'awesome_print'
gem 'byebug'
gem 'rspec-rails', '~> 3.8'
end
rails_helper.rb
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!
config.filter_rails_from_backtrace!
end
spec_helper.rb
RSpec.configure do |config|
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
config.shared_context_metadata_behavior = :apply_to_host_groups
end

Rspec undefined method `expects' for Object

I have recently upgraded my ruby on rails project to rails 5.0.7 and ruby 2.5.1 and I am getting an RSPEC error undefined methodexpects' for` different objects I am testing.
I have tried adding a configuration in the spec_helper.rb file as suggested here (although I did a quick search and didn't find :should defined anywhere), here and here:
config.expect_with :rspec do |expectations|
expectations.syntax = [:expect, :expects]
end
Also tried including include RSpec::Matchers in my spec_helper.rb, but then I get even more errors:
`only the `receive`, `have_received` and `receive_messages` matchers are supported with `expect(...).to`, but you have provided: #<RSpec::Matchers::BuiltIn::Eq:0x00007f962b3db058>`
An example of how I am using expect:
describe "process" do
before :each do
#item = Item.new
end
it "parses the uploaded file and extract the correct report data" do
item_a_data = {"name" => "one"}
item_b_data = {"name" => "two"}
file_contents = {"items" => [item_a_data, item_b_data]}
#item.data_file = double("file", :read => file_contents.to_json)
#item.name = item_a_data["name"]
#item.expects(:interpret_json_file).with(#item.data_file).returns(file_contents)
#item.expects(:save).returns(true)
expect(#item.process_data_file).to be_truthy
expect(#item.data).to eq item_a_data.to_json
end
Notice that the error occurs when I call #item.expects
In my Gemfile I have the following gems (among others):
group :development, :test do
gem 'byebug'
gem 'sqlite3'
gem 'rspec-rails'
gem 'rb-fsevent', '~> 0.9.1'
gem 'guard-rspec', '4.7.3'
gem 'guard-spork', '2.1.0'
gem 'spork', '0.9.2'
gem 'jasmine-rails'
gem 'teaspoon-jasmine'
end
group :test do
gem 'capybara'
gem 'cucumber-rails', :require => false
gem 'database_cleaner'
gem 'factory_girl_rails', '4.1.0'
gem 'launchy'
gem 'poltergeist'
gem 'timecop'
gem 'webmock'
gem 'simplecov', '~> 0.16.0'
gem 'rails-controller-testing'
end
spec_helper.rb:
require 'rubygems'
require 'spork'
#uncomment the following line to use spork with the debugger
#require 'spork/ext/ruby-debug'
Spork.prefork do
require 'simplecov'
SimpleCov.start 'rails'
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'devise'
require './spec/controllers/controller_helpers.rb'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.include Devise::Test::ControllerHelpers, type: :controller
config.include ControllerHelpers, type: :controller
config.include Capybara::DSL
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
config.infer_spec_type_from_file_location!
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = "random"
config.include FactoryGirl::Syntax::Methods
# config.expect_with :rspec do |expectations|
# expectations.syntax = :expect
# end
end
end
Spork.each_run do
# This code will be run each time you run your specs.
end
The solution would be to re-write it to the following syntax:
expect(#item).to receive(:interpret_json_file).with(#item.data_file) { file_contents }
expect(#item).to receive(:save) { true }
Basically ActiveRecord model does not know about rspec expectations and it should not. If that specs were working before then you might used some decoration for ActiveRecord models and that is why it worked previously.

ArgumentError in mongoid-rspec

When I do bundle exec rspec spec/controllers I got:
/var/lib/gems/1.9.1/gems/rspec-core-3.0.4/lib/rspec/core/hooks.rb:521:in `extract_scope_from': You must explicitly give a scope (example, context, suite) or scope alias (each, all) when using symbols as metadata for a hook. (ArgumentError)
My Gemfile:
gem 'rails', '4.1.4'
gem 'mongoid', '~> 4', github: 'mongoid/mongoid'
gem 'sass-rails', '~> 4.0.3'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'spring', group: :development
group :development, :test do
gem 'rspec-rails', '~> 3.0.2'
end
group :test do
gem 'database_cleaner'
gem 'factory_girl_rails'
gem 'mongoid-rspec'
end
My spec_helper.rb:
# spec_helper.rb
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.mock_with :rspec
config.before :each do
Mongoid.purge!
end
require 'database_cleaner'
config.before(:siute) do
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.orm = "mongoid"
end
config.before(:each) do
DatabaseCleaner.clean
end
end
My rails_helper.rb
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
RSpec.configure do |config|
# ArgumentError whether the following is commented out or not
config.infer_spec_type_from_file_location!
end
My spec/controllers/typer_controller_spec.rb (only file in the controllers folder)
require "rails_helper"
RSpec.describe TyperController, type:controller do
describe "GET #index" do
it "initializes properly" do
get :index
# nothing to test yet
end
end
end
You have a syntax error here
RSpec.describe TyperController, type:controller do
Change it to
RSpec.describe TyperController, type: :controller do
My mistake:
config.before(:siute) do
should be
config.before(:suite) do

Configure Rspec with Mongoid

I am running Rails 3 with Mongoid 3.
I am trying to remove active record and replace RSpec test with Mongoid
This is my config/test.
this is my spec_helper.rb
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
RSpec.configure do |config|
#config.fixture_path = "#{::Rails.root}/spec/fixtures"
#config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
config.before(:suite) do
DatabaseCleaner[:mongoid].strategy = :truncation
end
config.before(:each) do
DatabaseCleaner[:mongoid].start
end
config.after(:each) do
DatabaseCleaner[:mongoid].clean
end
end
This is my Gemfile (for dev and test)
group :development, :test do
gem 'sqlite3', '1.3.5'
gem 'rspec-rails', ">= 2.0.0.beta"
gem 'guard-rspec', '1.2.1'
gem 'guard-spork', '1.2.0'
gem 'childprocess', '0.3.6'
gem 'spork', '0.9.2'
end
group :test do
gem 'capybara', '1.1.2'
gem "database_cleaner", "~> 1.1.1"
gem 'factory_girl_rails', '4.1.0'
gem "rb-inotify", "~> 0.9.0"
gem "libnotify", "~> 0.8.0"
end
However when a simple array splitter test(no db action)
require 'spec_helper'
describe ArrayHelper do
describe "split array" do
it "splits array up to three" do
testArray = [1,2]
result = splitTasks(testArray)
result.should eq(Array(Array(1),Array(2)))
end
I get the error
`method_missing': undefined method `active_record' for #<Rails::Application::Configuration
Ideas?
end
end
I think you should comment out
require "active_record/railtie"
From within confing/application.rb file.
I hope this helps :)

RSpec can't find Factorys from Factorygirl

i will use RSpec with Factory girl in my Rails3 Project. I have installed factory girl but it don't find the factorys i have this error
Failure/Error: Factory.build(:user).should_be valid
No such factory: user
spec/factories/user_factory.rb :
Factory.define :user do |u|
u.username 'otto'
end
spec/spec_helper.rb
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'factory_girl'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.mock_with :rspec
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
end
Gemfile:
group :development, :test do
gem 'webrat'
gem "cucumber-rails"
gem "rspec-rails"
gem "rspec"
gem "autotest"
gem 'factory_girl'
end
Thanks
Do you have the following lines in your config\application.rb:
# Configure generators values.
config.generators do |g|
g.test_framework :rspec, :fixture => true
g.fixture_replacement :factory_girl, :dir=>"spec/factories"
end
Add the 'factory_girl_rails" gem to your Gemfile under your :test, :development groups, as follows:
group :development, :test do
gem 'webrat'
gem "cucumber-rails"
gem "rspec-rails"
gem "rspec"
gem "autotest"
gem 'factory_girl'
gem 'factory_girl_rails'
end
In Rails 3, you need to add that gem to make it work. Hope it helps.
have you tried adding something like
Dir[Rails.root.join("spec/factories/**/*.rb")].each {|f| require f}
in the spec_helper?
that did it for me
If your rspec config is loading everything under spec/support then you can put your factories dir there.
See this post: http://www.codeography.com/2010/04/30/using-factory_girl-and-rspec-with-rails-3.html

Resources