【问题标题】:Rails build fails in Travis due to undefined method 'encrypted_password=' for User由于未定义用户的方法'encrypted_pa​​ssword =',Rails 在 Travis 中构建失败
【发布时间】:2014-11-15 06:55:20
【问题描述】:

所以我有一个使用 Devise gem 的用户模型。

# == Schema Information
#
# Table name: USERS
#
#  u_id                   :integer          not null, primary key
#  email                  :string(255)      default(""), not null
#  encrypted_password     :string(255)      default(""), not null
#  reset_password_token   :string(255)
#  reset_password_sent_at :datetime
#  remember_created_at    :datetime
#  sign_in_count          :integer          default(0), not null
#  current_sign_in_at     :datetime
#  last_sign_in_at        :datetime
#  current_sign_in_ip     :string(255)
#  last_sign_in_ip        :string(255)
#  join_date              :datetime
#  organization           :string(255)
#  created_at             :datetime
#  updated_at             :datetime
#

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  self.table_name = "USERS"
end

我还有一个工厂女孩工厂来为我的 RSpec 测试创建用户对象

FactoryGirl.define do
  factory :user do |user|
    user.email 'test@example.com'
    user.password Devise.friendly_token[0,20]
  end
end

我在本地环境中使用rake spec 进行的所有测试都通过了,但是当我运行 Travis 构建时,它总是在 RSpec 测试中失败:

undefined method `encrypted_password=' for #<User:0x67c24931>
 # ./spec/models/user_spec.rb:26:in `(root)'

这是我的 user_spec.rb

require 'rails_helper'

RSpec.describe User, :type => :model do
  it "successfully creates a user" do
    @user = FactoryGirl.create :user
    expect(@user.save).to be_truthy
  end
end

我通过rails c 检查User.new.respond_to?(:encrypted_password=) 返回真。我不确定可能出了什么问题,也找不到任何问题的答案。那么,我错过了什么吗?

编辑:

忘记包含我的 .travis.yml

language: ruby
rvm:
    - jruby-1.7.15
env:
    - DB=mysql

script:
    - export JRUBY_OPTS=--1.9
    - RAILS_ENV=test bundle exec rake --trace db:migrate spec
before_script:
    - mysql -e 'create database test_db'

编辑2: 为数据库迁移添加 Travis 日志

$ RAILS_ENV=test bundle exec rake --trace db:migrate spec
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:migrate
== 20141031200108 CreateAllTables: migrating ==================================
-- create_table("GROUPS", {:primary_key=>"g_id", :force=>true})
   -> 0.0130s
-- create_table("TICKET_PRIORITY", {:primary_key=>"tp_id", :force=>true})
   -> 0.0120s
-- create_table("USERS", {:primary_key=>"u_id", :force=>true})
   -> 0.0110s
-- create_table("BOARDS", {:primary_key=>"b_id", :force=>true})
   -> 0.0120s
-- add_index("BOARDS", ["current_sprint_id"], {:name=>"current_sprint_id", :using=>:btree})
   -> 0.0370s
   -> 0 rows
-- add_index("BOARDS", ["p_id"], {:name=>"p_id", :using=>:btree})
   -> 0.0200s
   -> 0 rows
-- create_table("COMMENTS", {:primary_key=>"c_id", :force=>true})
   -> 0.0120s
-- add_index("COMMENTS", ["u_id"], {:name=>"u_id", :using=>:btree})
   -> 0.0210s
   -> 0 rows
-- create_table("GROUP_PERMISSIONS", {:id=>false, :force=>true})
   -> 0.0120s
-- add_index("GROUP_PERMISSIONS", ["g_id"], {:name=>"g_id", :using=>:btree})
   -> 0.0190s
   -> 0 rows
-- create_table("PROJECTS", {:primary_key=>"p_id", :force=>true})
   -> 0.0150s
-- add_index("PROJECTS", ["owner_id"], {:name=>"owner_id", :using=>:btree})
   -> 0.0190s
   -> 0 rows
-- create_table("SPRINTS", {:primary_key=>"s_id", :force=>true})
   -> 0.0110s
-- add_index("SPRINTS", ["b_id"], {:name=>"b_id", :using=>:btree})
   -> 0.0190s
   -> 0 rows
-- create_table("TICKET_ASSIGNMENT_HISTORY", {:id=>false, :force=>true})
   -> 0.0140s
-- add_index("TICKET_ASSIGNMENT_HISTORY", ["t_id"], {:name=>"t_id", :using=>:btree})
   -> 0.0210s
   -> 0 rows
-- add_index("TICKET_ASSIGNMENT_HISTORY", ["u_id"], {:name=>"u_id", :using=>:btree})
   -> 0.0210s
   -> 0 rows
-- create_table("TICKET_STATUS_HISTORY", {:primary_key=>"tsh_id", :force=>true})
   -> 0.0130s
-- add_index("TICKET_STATUS_HISTORY", ["t_id"], {:name=>"t_id", :using=>:btree})
   -> 0.0180s
   -> 0 rows
-- create_table("TICKET_TAGS", {:id=>false, :force=>true})
   -> 0.0120s
-- add_index("TICKET_TAGS", ["t_id"], {:name=>"t_id", :using=>:btree})
   -> 0.0170s
   -> 0 rows
-- create_table("TICKETS", {:primary_key=>"t_id", :force=>true})
   -> 0.0150s
-- add_index("TICKETS", ["b_id"], {:name=>"b_id", :using=>:btree})
   -> 0.0180s
   -> 0 rows
-- add_index("TICKETS", ["current_status_id"], {:name=>"current_status_id", :using=>:btree})
   -> 0.0180s
   -> 0 rows
-- add_index("TICKETS", ["s_id"], {:name=>"s_id", :using=>:btree})
   -> 0.0220s
   -> 0 rows
-- add_index("TICKETS", ["tp_id"], {:name=>"tp_id", :using=>:btree})
   -> 0.0220s
   -> 0 rows
-- create_table("USER_GROUP", {:id=>false, :force=>true})
   -> 0.0160s
-- add_index("USER_GROUP", ["g_id"], {:name=>"g_id", :using=>:btree})
   -> 0.0190s
   -> 0 rows
-- add_index("USER_GROUP", ["u_id"], {:name=>"u_id", :using=>:btree})
   -> 0.0180s
   -> 0 rows
== 20141031200108 CreateAllTables: migrated (0.5070s) =========================
== 20141031201446 UpdateEnumsInTables: migrating ==============================
-- remove_column(:TICKETS, :type, :string)
   -> 0.0290s
   -> 0 rows
-- add_column(:TICKETS, :type, :integer)
   -> 0.0250s
   -> 0 rows
-- remove_column(:TICKET_ASSIGNMENT_HISTORY, :relationship, :string)
   -> 0.0240s
   -> 0 rows
-- add_column(:TICKET_ASSIGNMENT_HISTORY, :relationship, :integer)
   -> 0.0280s
   -> 0 rows
-- remove_column(:TICKET_STATUS_HISTORY, :new_status, :string)
   -> 0.0250s
   -> 0 rows
-- add_column(:TICKET_STATUS_HISTORY, :new_status, :integer)
   -> 0.0250s
   -> 0 rows
== 20141031201446 UpdateEnumsInTables: migrated (0.1580s) =====================
== 20141106220707 ModifyGroupPermissions: migrating ===========================
-- remove_column(:GROUP_PERMISSIONS, :name)
   -> 0.0230s
   -> 0 rows
-- remove_column(:GROUP_PERMISSIONS, :rank)
   -> 0.0250s
   -> 0 rows
-- add_column(:GROUP_PERMISSIONS, :pm_id, :integer)
   -> 0.0280s
   -> 0 rows
-- create_table("PERMISSIONS", {:primary_key=>"pm_id", :force=>true})
   -> 0.0110s
== 20141106220707 ModifyGroupPermissions: migrated (0.0880s) ==================
== 20141107011729 DeviseCreateUsers: migrating ================================
-- create_table(:users, {:primary_key=>"u_id", :force=>true})
   -> 0.0160s
-- add_index(:users, :email, {:unique=>true})
   -> 0.0350s
   -> 0 rows
-- add_index(:users, :reset_password_token, {:unique=>true})
   -> 0.0200s
   -> 0 rows
== 20141107011729 DeviseCreateUsers: migrated (0.0790s) =======================
** 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 spec (first_time)
** Invoke spec:prepare (first_time)
** Execute spec:prepare
** Invoke test:prepare (first_time)
** Execute test:prepare
** Execute spec

编辑:

我还尝试在 travis 上运行 rake spec 之前运行 mysql -uroot -e 'use test_db; describe users;',它显示用户表具有 encrypted_pa​​ssword 列。我还运行了cat db/schema.rb 以查看迁移是否完全发生,并且 encrypted_pa​​ssword 也在那里。现在我不确定什么是错误的,因为该字段在数据库中。

【问题讨论】:

  • 问题可能出在迁移测试数据库上。显示你的 travis 日志
  • 添加 Travis 数据库迁移日志
  • -- create_table("USERS", {:primary_key=>"u_id", :force=>true}) 此迁移是否包含缺失列?
  • 是的。我什至在 travis 中运行 rake spec 之前运行了 mysql -e 'use test_db; describe users;',它显示了在那里被投诉的专栏。所以它在测试运行之前就存在于 test_db 中。

标签: ruby-on-rails rspec devise travis-ci


【解决方案1】:

好吧,当我在 MySQL 中创建表时,我对所有表名都使用了大写名称。由于某种原因,在本地运行时,表名在开发模式下仍然是大写的,但在测试模式下是小写的。因此,在我的用户模型中,我只是将 self.table_name = "USERS" 更改为 self.table_name = "users" 并解决了我的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多