Sha256: 503fd51ee7203a306c34f0e5c01815124f13eb16de46b22bc9cb5d0900a739e1

Contents?: true

Size: 1.75 KB

Versions: 16

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

module AcceptanceSpecHelpers
  def generate_model(model_name, *fields)
    Rails::Generators.invoke('declare_schema:model', [model_name, *fields])
  end

  def generate_migrations(*flags)
    Rails::Generators.invoke('declare_schema:migration', flags)
  end

  def expect_model_definition_to_eq(model, expectation)
    expect_file_to_eq("#{TESTAPP_PATH}/app/models/#{model}.rb", expectation)
  end

  def expect_test_definition_to_eq(model, expectation)
    expect_file_to_eq("#{TESTAPP_PATH}/test/models/#{model}_test.rb", expectation)
  end

  def expect_test_fixture_to_eq(model, expectation)
    expect_file_to_eq("#{TESTAPP_PATH}/test/fixtures/#{model}.yml", expectation)
  end

  def expect_file_to_eq(file_path, expectation)
    expect(File.exist?(file_path)).to be_truthy
    expect(File.read(file_path).gsub(/require '([^']*)'/, 'require "\1"')).to eq(expectation)
  end

  def clean_up_model(model)
    system("rm -rf #{TESTAPP_PATH}/app/models/#{model}.rb #{TESTAPP_PATH}/test/models/#{model}.rb #{TESTAPP_PATH}/test/fixtures/#{model}.rb")
  end

  def load_models
    Rails.application.config.autoload_paths += ["#{TESTAPP_PATH}/app/models"]
    $LOAD_PATH << "#{TESTAPP_PATH}/app/models"
  end

  def migrate_up(expected_value)
    MigrationUpEquals.new(expected_value)
  end

  def migrate_down(expected_value)
    MigrationDownEquals.new(expected_value)
  end

  class MigrationUpEquals < RSpec::Matchers::BuiltIn::Eq
    def matches?(subject)
      super(subject[0].gsub(/, +([a-z_]+:)/i, ', \1')) # normalize multiple spaces to one
    end
  end

  class MigrationDownEquals < RSpec::Matchers::BuiltIn::Eq
    def matches?(subject)
      super(subject[1].gsub(/, +([a-z_]+:)/i, ', \1')) # normalize multiple spaces to one
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
declare_schema-0.10.0 spec/support/acceptance_spec_helpers.rb
declare_schema-0.10.0.pre.dc.1 spec/support/acceptance_spec_helpers.rb
declare_schema-0.9.0 spec/support/acceptance_spec_helpers.rb
declare_schema-0.8.0 spec/support/acceptance_spec_helpers.rb
declare_schema-0.8.0.pre.6 spec/support/acceptance_spec_helpers.rb
declare_schema-0.8.0.pre.5 spec/support/acceptance_spec_helpers.rb
declare_schema-0.8.0.pre.4 spec/support/acceptance_spec_helpers.rb
declare_schema-0.8.0.pre.3 spec/support/acceptance_spec_helpers.rb
declare_schema-0.8.0.pre.2 spec/support/acceptance_spec_helpers.rb
declare_schema-0.8.0.pre.1 spec/support/acceptance_spec_helpers.rb
declare_schema-0.7.1 spec/support/acceptance_spec_helpers.rb
declare_schema-0.7.0 spec/support/acceptance_spec_helpers.rb
declare_schema-0.6.4 spec/support/acceptance_spec_helpers.rb
declare_schema-0.6.3 spec/support/acceptance_spec_helpers.rb
declare_schema-0.6.2 spec/support/acceptance_spec_helpers.rb
declare_schema-0.6.1 spec/support/acceptance_spec_helpers.rb