Search code examples
ruby-on-railsrspecruby-on-rails-5rspec-rails

Rspec loading old schema


I am working on a legacy project (recently upgraded from rails 4.1 to 5.2) and I had to change an association table. Before: reports had many clients and clients had many reports. Now I have created a ClientsReport table than not only holds the client_id and reports_id but also has an id as primary_key and a can_manage (boolean).

Testing with rspec it is giving me an error when calling reports_clients.for(report).first.can_manage saying unKnownAttribute can_manage for <ClientsReport client_id: 1, report_id:3> no sign if id nor can_manage

So it looks like it is using the old schema.

Also tried adding ActiveRecord::Migration.maintain_test_schema! as suggested here but I am not sure

I tried running rake:db:prepare but it threw me a bunch of errors and now looks like I broke the test db, as I was having 4 failing tests and now I have 166...

My spec_helper.rb looks like this:

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

    # ## Mock Framework
    #
    # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
    #
    # config.mock_with :mocha
    # config.mock_with :flexmock
    # config.mock_with :rr

    # 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
  end

end


Spork.each_run do
  # This code will be run each time you run your specs.
end

Solution

  • You can completely reset the test DB with this command, after making sure your schema is up to date.

    bin/rails db:environment:set db:drop db:create db:schema:load RAILS_ENV=test