1

测试套件在 local 上通过,但在 Travis-CI 上失败。这是来自失败的构建的片段:

Failures:
  1) account creation allows access of subdomain
     Failure/Error: @account.save

     ActiveRecord::StatementInvalid:
       PG::UndefinedTable: ERROR:  relation "users" does not exist at character 13
       : INSERT INTO "users" ("username", "email", "encrypted_password", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"
     # ./app/controllers/accounts_controller.rb:14:in `create'
     # ./spec/features/account_creation_feature_spec.rb:43:in `sign_up'
     # ./spec/features/account_creation_feature_spec.rb:6:in `block (2 levels) in <top (required)>'
     # ------------------
     # --- Caused by: ---
     # PG::UndefinedTable:
     #   ERROR:  relation "users" does not exist at character 13
     #   ./app/controllers/accounts_controller.rb:14:in `create'

顺便说一下,这是规范,失败的示例数量各不相同;有时只有一个会失败,两个或全部失败。无论是否需要 spec_helper,在 CI 上仍然失败......

account_creation_feature_spec.rb

require 'rails_helper'
# require 'spec_helper'

describe 'account creation' do
  let(:subdomain) { FactoryGirl.generate(:subdomain) }
  before(:each) { sign_up(subdomain) }

  it 'allows user to create account' do
    expect(page.current_url).to include(subdomain)
    expect(Account.all.count).to eq(1)
  end

  it 'allows access of subdomain' do
    visit root_url(subdomain: subdomain)
    puts page.current_url
    expect(page.current_url).to include(subdomain)
  end

  it 'allows account followup creation' do
    subdomain2 = "#{subdomain}2"
    sign_up(subdomain2)
    expect(page.current_url).to include(subdomain2)
    expect(Account.all.count).to eq(2)
  end

  it 'does not allow account creation on subdomain' do
    user = User.first
    subdomain = Account.first.subdomain
    sign_in_user(user, subdomain: subdomain)
    expect { visit new_account_url(subdomain: subdomain) }.to raise_error ActionController::RoutingError
  end

  def sign_up(subdomain)
    visit root_url(subdomain: false)
    click_link 'Create Account'
    # puts page.html

    fill_in 'account_owner_attributes_username', with: 'Indy'
    fill_in 'account_owner_attributes_email', with: 'indiana@jones.com'
    fill_in 'account_owner_attributes_password', with: 'templeofdoom'
    fill_in 'account_owner_attributes_password_confirmation', with: 'templeofdoom'
    fill_in 'Subdomain', with: subdomain
    click_button 'Create Account'
  end
end

这与 travs.yml 的设置方式有什么关系吗?

travis.yml

language: ruby
rvm:
  - 2.3.3

sudo: required

services:
  - postgresql

cache:
  directories:
    - travis_phantomjs

before_install:
  - "export PATH=$PWD/travis_phantomjs/phantomjs-2.1.1-linux-x86_64/bin:$PATH"
  - "if [ $(phantomjs --version) != '2.1.1' ]; then rm -rf $PWD/travis_phantomjs; mkdir -p $PWD/travis_phantomjs; fi"
  - "if [ $(phantomjs --version) != '2.1.1' ]; then wget https://assets.membergetmember.co/software/phantomjs-2.1.1-linux-x86_64.tar.bz2 -O $PWD/travis_phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2; fi"
  - "if [ $(phantomjs --version) != '2.1.1' ]; then tar -xvf $PWD/travis_phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2 -C $PWD/travis_phantomjs; fi"
  - "phantomjs --version"

before_script:
  - cp config/database.yml.travis config/database.yml
  - psql -c 'create database travis_ci_test;' -U postgres
  - bundle exec rake db:setup
  - bin/rake db:migrate RAILS_ENV=test

script:
  - bundle exec rspec

或者这是 factory_girl 的问题?

规格/工厂/accounts.rb

FactoryGirl.define do
  sequence(:subdomain) { |n| "subdomain#{n}" }

  factory :account do
    sequence(:subdomain) { |n| "subdomain#{n}" }
    association :owner, factory: :user

    factory :account_with_schema do
      after(:build) do |account|
        Apartment::Tenant.create(account.subdomain)
        Apartment::Tenant.switch!(account.subdomain)
      end
      after(:create) do |account|
        Apartment::Tenant.reset
      end
    end
  end
end

相关的宝石可能是设计、工厂女孩和公寓......

宝石文件

source 'https://rubygems.org'

gem 'rails', '4.2.8'
# Use postgresql as the database for Active Record
gem 'pg', '~> 0.15'
# Sass-powered version of Bootstrap 3
gem 'bootstrap-sass', '~> 3.3.6'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
gem 'sprockets-rails', '>= 2.1.4'
gem 'simple_form'
gem 'devise'
# Database multi-tenancy for Rails applications.
gem 'apartment'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# Use jquery as the JavaScript library
gem 'jquery-rails'

# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Unicorn as the app server
# gem 'unicorn'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
group :test do
  # Powerful matchers for testing models & controllers.
  gem 'shoulda-matchers', require: false
  # Ruby wrapper for PhantomJS headless browser.
  gem 'poltergeist'
end

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  # gem 'byebug'
  gem 'guard'
  # Auto-reload browser when files are saved.
  gem 'guard-livereload'
  # Run tests when files are saved.
  gem 'guard-rspec'
  gem 'rspec-rails'
  # Integration testing for Rails.
  gem 'capybara'
  # Factories for instances of an ActiveRecord object.
  gem 'factory_girl_rails', require: false
  # Clean up the database after test runs.
  gem 'database_cleaner'
  # Evaluate test coverage.
  gem 'coveralls', require: false
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> in views
  gem 'web-console', '~> 2.0'

  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
end

# Speed up page load times, and see Heroku Log errors.
gem 'rails_12factor', group: :production

更新

运行该命令rake db:migrate --trace会产生以下结果:

** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:migrate
** Invoke db:_dump (first_time)
** Execute db:_dump
** Invoke db:schema:dump (first_time)
** Invoke environment 
** Invoke db:load_config 
** Execute db:schema:dump
** Invoke apartment:migrate (first_time)
** Execute apartment:migrate
        [WARNING] - The list of tenants to migrate appears to be empty. This could mean a few things:

          1. You may not have created any, in which case you can ignore this message
          2. You've run `apartment:migrate` directly without loading the Rails environment
            * `apartment:migrate` is now deprecated. Tenants will automatically be migrated with `db:migrate`

        Note that your tenants currently haven't been migrated. You'll need to run `db:migrate` to rectify this.

只运行 db:migrate 将发出相同的警告。

4

1 回答 1

0

以下提供了一些成功的衡量标准,但是Travis CI 上的测试继续(间歇性地)失败。这不是最终解决方案。我调整了几个文件并采取了以下步骤:

1.在终端中,运行以下命令:

bundle exec rake secret

2.复制生成的密钥

3.打开config/initializers/devise.rb
下面Devise.setup do |config|

config.secret_key = 'paste generated key from the terminal here'

4.将以下内容添加到travis.yml

env: RAILS_ENV=test SECRET_KEY_BASE="$(bundle exec rake secret)"

于 2017-04-22T15:36:19.830 回答