I am trying to set up the rails testing framework but am facing some issues. My setup is as follows
test/models/clinic_test.rb
require 'test_helper'
class ClinicTest < ActiveSupport::TestCase
test "sample" do
clinic = clinics(:myclinic)
assert(clinic.name == 'Krishan')
end
end
test/fixtures/clinics.yml
myclinic:
name: Krishan
But when I run the clinic_test rake process I get the following error:
ActiveRecord::FixtureClassNotFound: No class attached to find
test/models/clinic_test.rb:5:in `block in <class:ClinicTest>'
I see that the database is actually populated with the sample data from the clinics.yml file.
Where is the problem? Is this some configuration issue?
Add following lines in test_helper.rb file, before fixtures :all
class ActiveSupport::TestCase
self.use_transactional_fixtures = true
set_fixture_class clinics: Clinic
fixtures :all
...
end
clinics is the name of yml file where Clinic is the name of model.
Related
I am trying to write a test to check a user flow in my application. For that, I need to test a method called is_apple that is in AppleController, AppleController is inherited from FruitsController. I have a method in FruitsController called taste which can be used inside AppleController's is_apple method. The output is the same as expected in the Development and Production environment. But In the Test environment, The method from the FruitsController is undefined in AppleController. I am new to rails testing, I don't know what is potentially causing this issue in my application's test environment.
test_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
def setup
puts "starting a test"
post "v1/is_apple", params: {colour: 'red', taste: 'sweet'}
end
end
apple_controller.rb
module V1
class AppleController < FruitsController
def is_apple
return true if taste and params[:colour].eql?('red')
end
end
end
fruits_controller.rb
module V1
class FruitsController < ActionController::API
def taste
# assume
return true
end
end
end
I am using minitest '5.10.3' and rails 6. When I run rails test, am getting undefined_method 'taste' in AppleController. Is there any mistakes in my code? , If yes, How can I achieve the behaviour I wanted. Thanks in advance!
I'm trying to do some tests with my multi-tenancy rails app but getting into trouble with my fixtures. In my controller test I have the following setup:
setup do
Apartment::Tenant.create('test')
Apartment::Tenant.switch! 'test'
host! "test:3000"
sign_in users(:admin)
end
When running the test I get this Error:
ActiveRecord::RecordNotFound: Couldn't find User with 'id'=255947101
I think the problem is that the fixtures are being created before switching to the test tenant. How do I create the fixtures after switching the tenant?
Ran into this same problem this morning ... I was able to get around it, at least for now, with this change in my test_helper.rb file:
# Customizing the base TestCase
module ActiveSupport
class TestCase
Apartment::Tenant.create('the-tenant')
Apartment::Tenant.switch! 'the-tenant'
fixtures :all
ActiveRecord::Migration.check_pending!
def setup
Capybara.app_host = 'http://www.my-somain.local'
DatabaseCleaner.strategy = :transaction
end
def teardown
Apartment::Tenant.drop('the-tenant')
Apartment::Tenant.reset
DatabaseCleaner.clean
end
end
end
Basically, I moved the tenant creation outside the setup method, where it previously resided. This seems to have fixed the issue with the fixtures being created outside the tenant on which I am testing.
I hope this helps ... not 100% sure it's ideal but it does have my tests running today :)
1) created a model called Skill
2) ran some seeds
3) ran rspec --init
4) created file skill_spec.rb with the code below
require_relative "../app/models/skill"
describe Skill do
describe "database" do
it "should have 42 skills" do
expect(Skill.all.count).to eq(42)
end
end
end
5) when I run rspec in console get error:
Failure/Error: class Skill < ApplicationRecordNameError:
uninitialized constant ApplicationRecord
I already have a file application_record.rb with the following code
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
For rails specs use require 'rails-helper' at beginning of each spec file (it is generated by bin/rails generate rspec:install from rspec-rails gem)
It contains line require File.expand_path('../config/environment', __dir__) that will load your rails environment and you'll have autoloading and all other rails parts working.
I've created a custom environment in the Rails app I'm working on called nhl_test. The models for this environment are in app/models/nhl namespace. There are a number of other models in app/models/[other_subdir] that I don't want to autoload in this environment. So far I tried modifying the config.paths in my environment file similarly to how I modified the db/migrate location, like so:
config/environments/nhl_test.rb
MyApp::Application.configure do
config.eager_load = false
config.paths["db/migrate"] = ["db/migrate/nhl"]
config.paths["app/models"] = ["app/models/nhl"]
end
However, the other subdirectories of app/models are still being loaded. I can tell because there is code in those models that will break in this environment and I'm unable to run my tests using RAILS_ENV=nhl_test m test/models/nhl - they break with a stack trace that points to app/models/mlb/base.rb.
How can I keep any of the models except what's in app/models/nhl from being loaded in this environment??
EDIT It turns out it's this line in test_helper.rb that is causing the problem:
class ActiveSupport::TestCase
fixtures :all
end
Rather than loading all fixtures, I just need to load the fixtures in test/fixtures/nhl somehow...
I've tried the following but it doesn't seem to be working:
class ActiveSupport::TestCase
fixture_path = Rails.root.join('test', 'fixtures', 'nhl')
fixtures :all
end
I was able to fix the problem by setting the fixture_path this way:
# test_helper.rb
ActiveSupport::TestCase.fixture_path = Rails.root.join('test', 'fixtures', Rails.env.gsub('_test', ''))
class ActiveSupport::TestCase
fixtures :all
end
I'm trying to write a custom Rake task to perform some tests for a class placed in the lib directory. This works for basic tests not requiring any models, but I need to actually test using some models. It's my first foray into more advanced rake usage and after going through some other hurdles I've got stuck on getting a ConnectionNotEstablished error.
Here's the rake task:
Rake::TestTask.new(:test => 'db:test:prepare') do |test|
test.libs << 'test/sync'
test.test_files = Dir['test/sync/*_test.rb']
test.verbose = true
end
Here's the test that raise the ConnectionNotEstablished exception:
require 'rubygems'
require 'app/models/city'
require 'foo'
require 'test/unit'
class SyncTest < Test::Unit::TestCase
##sync = Foo::Sync.new('test')
def test_cities
assert City.all.size == 2 # the exception is raised at this point
##sync.cities
assert City.all.size == 102
end
end
Here's a unit test that is actually working:
require 'test_helper'
class CityTest < ActiveSupport::TestCase
test "the truth" do
assert City.all.size == 2
end
end
I've tried using derive my test class from ActiveSupport::TestCase instead of Test::Unit::TestCase but it still raise a ConnectionNotEstablished error. I'm certainly doing something wrong, can anyone find what or tell of a better way to do that?
I've finally found out what the problem was, I've replaced the two first require call by:
require 'test_helper'
and added the test directory to the TestTask's libs attribute:
test.libs << 'test'