19

sorry for disturb you but i have a lot of question about this error. first this is my user_controller rspec file

require 'spec/spec_helper'

describe UserController do

it "create new user" do
    post "create"
    assigns[:users].should_not be_new_record
end
end

this is my UserController

 class UserController < ApplicationController
   def create
     @users = User.new
     if @users.save
       flash[:notice] = 'new user was successfully created.'
     else
       render :action => :new
     end
   end

   def new
     @user = User.new
   end
 end

and my routes.rb (i think the problem is here, excuse me but i`m new in this language)

Estaciones::Application.routes.draw do
  devise_for :users

  root :to => "home#index"
  resources :user
end

when i try to test my user_controller_rspec then i get this error

Failures:

1) UserController create new user
   Failure/Error: post "create"
   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
 # ./app/controllers/user_controller.rb:3:in `new'
 # ./app/controllers/user_controller.rb:3:in `create'
 # ./spec/controllers/user_controller_spec.rb:6

 Finished in 0.01722 seconds
 1 example, 1 failure

 Failed examples:

 rspec ./spec/controllers/user_controller_spec.rb:5 # UserController create new user

how i can fix it... thanks

drhenner
  • 2,200
  • 18
  • 23
Asantoya
  • 247
  • 1
  • 4
  • 12

2 Answers2

45

run this

 rake db:migrate

then this

 rake db:test:prepare
drhenner
  • 2,200
  • 18
  • 23
  • Yes you need to do this for rspec – drhenner Jul 17 '12 at 19:55
  • 1
    I editted your question to look like real code... Next time add 4 spaces before any code. – drhenner Jul 17 '12 at 19:56
  • ok thanks.. i tried the solution thah you gave me now in the promopt appears this sorry if i bother you Failure/Error: post :create ActionView::MissingTemplate: Missing template user/new, application/new with {:handlers=>[:haml, :builder, :coffee, :erb], :locale=>[:en], :formats=>[:html]}. Searched in: * "#" – Asantoya Jul 17 '12 at 19:58
  • 1
    first... why do you have a user_controller instead of users_controller? Second, do you have a app/views/user/new.html.erb file? – drhenner Jul 18 '12 at 04:30
  • Same error here on heroku deploy caused by push after new user resource created. The command with heroku toolbelt is `heroku run rake db:migrate` – isimmons Sep 24 '13 at 22:06
3

Alternative variant

RAILS_ENV=test rake db:migrate

Sergiy Seletskyy
  • 16,236
  • 7
  • 69
  • 80