Topic: strangre failer [SOLVED]

i am making a test for my model

MODEL

# == Schema Information
#
# Table name: users
#
#  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)
#  current_sign_in_at     :datetime
#  last_sign_in_at        :datetime
#  current_sign_in_ip     :string(255)
#  last_sign_in_ip        :string(255)
#  created_at             :datetime        not null
#  updated_at             :datetime        not null
#  username               :string(255)
#  account_id             :integer
#  first_name             :string(255)
#  last_name              :string(255)
#

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :first_name, :last_name, :account_id
  # attr_accessible :title, :body
--
#  validates_uniqueness_of :email, presence: true, :case_sensitive => false
  validates_uniqueness_of :username, presence: true, :case_sensitive => false


  validates :first_name, presence: true
  validates :last_name, presence: true
end

TEST

require 'spec_helper'
describe User do
  before do
    @user = User.new( email: "moisesz@circletechfl.com", password: "foobar", password_confirmation: "foobar", remember_me: true, username: "moiseszaragoza", first_name: "Moises", last_name: "Zaragoza", account_id: 1)
  end
   subject { @user }
   it { should respond_to(:email) }
end

Failures:

  1) User
     Failure/Error: @user = User.new( email: "moisesz@circletechfl.com", password: "foobar", password_confirmation: "foobar", remember_me: true, username: "moiseszaragoza", first_name: "Moises", last_name: "Zaragoza", account_id: 1)
     ActiveRecord::StatementInvalid:
       PG::Error: ERROR:  relation "users" does not exist
       LINE 4:              WHERE a.attrelid = '"users"'::regclass
                                               ^
       :             SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
                     FROM pg_attribute a LEFT JOIN pg_attrdef d
                       ON a.attrelid = d.adrelid AND a.attnum = d.adnum
                    WHERE a.attrelid = '"users"'::regclass
                      AND a.attnum > 0 AND NOT a.attisdropped
                    ORDER BY a.attnum
     # ./spec/models/user_spec.rb:4:in `new'
     # ./spec/models/user_spec.rb:4:in `block (2 levels) in <top (required)>'

Finished in 0.11132 seconds
1 examples, 1 failure

Failed examples:

rspec ./spec/models/user_spec.rb:7 # User


rails c

rails c
Loading development environment (Rails 3.2.3)
1.9.3p125 :001 > @user = User.new( email: "moisesz@circletechfl.com", password: "foobar", password_confirmation: "foobar", remember_me: true, username: "moiseszaragoza", first_name: "Moises", last_name: "Zaragoza", account_id: 1)
 => #<User id: nil, email: "moisesz@circletechfl.com", encrypted_password: "$2a$10$WMrpi62jS10/5MjbR5fNE./txLuc.zLrbZled1Oynw9g...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: nil, updated_at: nil, username: "moiseszaragoza", account_id: 1, first_name: "Moises", last_name: "Zaragoza"> 
1.9.3p125 :002 > @user.save
   (0.2ms)  BEGIN
  User Exists (0.8ms)  SELECT 1 FROM "users" WHERE "users"."email" = 'moisesz@circletechfl.com' LIMIT 1
  User Exists (0.3ms)  SELECT 1 FROM "users" WHERE LOWER("users"."username") = LOWER('moiseszaragoza') LIMIT 1
  SQL (8.5ms)  INSERT INTO "users" ("account_id", "created_at", "current_sign_in_at", "current_sign_in_ip", "email", "encrypted_password", "first_name", "last_name", "last_sign_in_at", "last_sign_in_ip", "remember_created_at", "reset_password_sent_at", "reset_password_token", "sign_in_count", "updated_at", "username") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16) RETURNING "id"  [["account_id", 1], ["created_at", Tue, 22 May 2012 19:05:16 UTC +00:00], ["current_sign_in_at", nil], ["current_sign_in_ip", nil], ["email", "moisesz@circletechfl.com"], ["encrypted_password", "$2a$10$WMrpi62jS10/5MjbR5fNE./txLuc.zLrbZled1Oynw9gnVUH1Nza."], ["first_name", "Moises"], ["last_name", "Zaragoza"], ["last_sign_in_at", nil], ["last_sign_in_ip", nil], ["remember_created_at", nil], ["reset_password_sent_at", nil], ["reset_password_token", nil], ["sign_in_count", 0], ["updated_at", Tue, 22 May 2012 19:05:16 UTC +00:00], ["username", "moiseszaragoza"]]
   (0.5ms)  COMMIT
 => true 

Last edited by moiseszaragoza (2012-05-22 18:15:06)

Re: strangre failer [SOLVED]

the problem here is that the TEST DB was not in place
just run

bundle exec rake db:test:prepare