由于未定义的方法而导致Rails构建在Travis中失败' encrypted_pa​​ssword ='对于用户

时间:2014-11-15 06:55:21

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

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

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

我还有一个Factory女工厂为我的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=)是否返回true。我不确定会出现什么问题,我无法找到问题的答案。那么,我错过了什么吗?

编辑:

忘记包含我的.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'

EDIT2: 添加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上运行mysql -uroot -e 'use test_db; describe users;'之前运行rake spec,并显示用户表具有encrypted_pa​​ssword列。我还运行cat db/schema.rb以查看迁移是否完全发生,并且encrypted_pa​​ssword也在那里。现在,由于该字段位于数据库中,我不确定会出现什么问题。

1 个答案:

答案 0 :(得分:0)

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