Here my issue:
I've got two models:
class User < ActiveRecord::Base
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :username
has_many :tasks
end
class Task < ActiveRecord::Base
belongs_to :user
end
with this simple routes.rb file
TestProj::Application.routes.draw do |map|
resources :users do
resources :tasks
end
end
this schema:
ActiveRecord::Schema.define(:version => 20100525021007) do
create_table "tasks", :force => true do |t|
t.string "name"
t.integer "estimated_time"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
end
create_table "users", :force => true do |t|
t.string "email"
t.string "password"
t.string "password_confirmation"
t.datetime "created_at"
t.datetime "updated_at"
t.string "username"
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
add_index "users", ["username"], :name => "index_users_on_username", :unique => true
end
and this controller for my tasks:
class TasksController < ApplicationController
before_filter :load_user
def new
@task = @user.tasks.new
end
private
def load_user
@user = User.find(params[:user_id])
end
end
Finally here is my test:
require 'spec_helper'
describe TasksController do
before(:each) do
@user = Factory(:user)
@task = Factory(:task)
end
#GET New
describe "GET New" do
before(:each) do
User.stub!(:find).with(@user.id.to_s).and_return(@user)
@user.stub_chain(:tasks, :new).and_return(@task)
end
it "should return a new Task" do
@user.tasks.should_receive(:new).and_return(@task)
get :new, :user_id => @user.id
end
end
end
This test fails with the following output:
1) TasksController GET New should return a new Task
Failure/Error: get :new, :user_id => @user.id
undefined method `abstract_class?' for Object:Class
# /home/chopper/.rvm/gems/ruby-1.8.7-p249@rails3/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/activerecord/lib/active_record/base.rb:1234:in `class_of_ac开发者_开发问答tive_record_descendant'
# /home/chopper/.rvm/gems/ruby-1.8.7-p249@rails3/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/activerecord/lib/active_record/base.rb:900:in `base_class'
# /home/chopper/.rvm/gems/ruby-1.8.7-p249@rails3/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/activerecord/lib/active_record/base.rb:655:in `reset_table_name'
# /home/chopper/.rvm/gems/ruby-1.8.7-p249@rails3/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/activerecord/lib/active_record/base.rb:647:in `table_name'
# /home/chopper/.rvm/gems/ruby-1.8.7-p249@rails3/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/activerecord/lib/active_record/base.rb:932:in `arel_table'
# /home/chopper/.rvm/gems/ruby-1.8.7-p249@rails3/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/activerecord/lib/active_record/base.rb:927:in `unscoped'
# /home/chopper/.rvm/gems/ruby-1.8.7-p249@rails3/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/activerecord/lib/active_record/named_scope.rb:30:in `scoped'
# /home/chopper/.rvm/gems/ruby-1.8.7-p249@rails3/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/activerecord/lib/active_record/base.rb:405:in `find'
# ./app/controllers/tasks_controller.rb:15:in `load_user'
# /home/chopper/.rvm/gems/ruby-1.8.7-p249@rails3/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/activesupport/lib/active_support/callbacks.rb:431:in `_run__1954900289__process_action__943997142__callbacks'
# /home/chopper/.rvm/gems/ruby-1.8.7-p249@rails3/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/activesupport/lib/active_support/callbacks.rb:405:in `send'
# /home/chopper/.rvm/gems/ruby-1.8.7-p249@rails3/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/activesupport/lib/active_support/callbacks.rb:405:in `_run_process_action_callbacks'
# /home/chopper/.rvm/gems/ruby-1.8.7-p249@rails3/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/activesupport/lib/active_support/callbacks.rb:88:in `send'
# /home/chopper/.rvm/gems/ruby-1.8.7-p249@rails3/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/activesupport/lib/active_support/callbacks.rb:88:in `run_callbacks'
# /home/chopper/.rvm/gems/ruby-1.8.7-p249@rails3/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/actionpack/lib/abstract_controller/callbacks.rb:17:in `process_action'
# /home/chopper/.rvm/gems/ruby-1.8.7-p249@rails3/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/actionpack/lib/action_controller/metal/rescue.rb:8:in `process_action'
# /home/chopper/.rvm/gems/ruby-1.8.7-p249@rails3/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/actionpack/lib/abstract_controller/base.rb:113:in `process'
# /home/chopper/.rvm/gems/ruby-1.8.7-p249@rails3/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/actionpack/lib/abstract_controller/rendering.rb:39:in `sass_old_process'
# /home/chopper/.rvm/gems/ruby-1.8.7-p249@rails3/gems/haml-3.0.0.beta.3/lib/sass/plugin/rails.rb:26:in `process'
# /home/chopper/.rvm/gems/ruby-1.8.7-p249@rails3/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/actionpack/lib/action_controller/metal/testing.rb:12:in `process_with_new_base_test'
# /home/chopper/.rvm/gems/ruby-1.8.7-p249@rails3/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/actionpack/lib/action_controller/test_case.rb:390:in `process'
# /home/chopper/.rvm/gems/ruby-1.8.7-p249@rails3/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/actionpack/lib/action_controller/test_case.rb:328:in `get'
# ./spec/controllers/tasks_controller_spec.rb:20
# /home/chopper/.rvm/gems/ruby-1.8.7-p249@rails3/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/activesupport/lib/active_support/dependencies.rb:209:in `inject'
Can anybody help me understand what's going on here? It seems to be an RSpec problem since the controller action actually works, but I could be wrong.
I recreated your project from scratch using the information you supplied, and the controller spec runs without any failures (Rails 2.3.5, Ruby 1.8.7). So I'm guessing there's something amiss in your project and/or Rails configuration? Sorry, I know that isn't much to go on.
I recently got this error message with my Rails 3 app using rspec-rails 2.0.0.beta22 (under Ruby 1.9.2). After lots of hair pulling I upgraded to the recently releases rspec-rails 2.0.0.rc and the error disappeared. So, I'd recommend upgrading your rspec-rails gem.
精彩评论