2010-05-31 66 views
1

这里我的问题:RSpec的测试失败寻找一套新的眼睛

我有两个型号:

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 

这个简单的routes.rb文件

TestProj::Application.routes.draw do |map| 

resources :users do 
    resources :tasks 
end 
end 

这模式:

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 

和这个控制器为m Ÿ任务:

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 

最后,这里是我的测试:

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 

此测试失败,出现以下的输出:

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/[email protected]/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/activerecord/lib/active_record/base.rb:1234:in `class_of_active_record_descendant' 
# /home/chopper/.rvm/gems/[email protected]/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/activerecord/lib/active_record/base.rb:900:in `base_class' 
# /home/chopper/.rvm/gems/[email protected]/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/activerecord/lib/active_record/base.rb:655:in `reset_table_name' 
# /home/chopper/.rvm/gems/[email protected]/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/activerecord/lib/active_record/base.rb:647:in `table_name' 
# /home/chopper/.rvm/gems/[email protected]/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/activerecord/lib/active_record/base.rb:932:in `arel_table' 
# /home/chopper/.rvm/gems/[email protected]/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/activerecord/lib/active_record/base.rb:927:in `unscoped' 
# /home/chopper/.rvm/gems/[email protected]/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/activerecord/lib/active_record/named_scope.rb:30:in `scoped' 
# /home/chopper/.rvm/gems/[email protected]/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/[email protected]/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/activesupport/lib/active_support/callbacks.rb:431:in `_run__1954900289__process_action__943997142__callbacks' 
# /home/chopper/.rvm/gems/[email protected]/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/activesupport/lib/active_support/callbacks.rb:405:in `send' 
# /home/chopper/.rvm/gems/[email protected]/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/activesupport/lib/active_support/callbacks.rb:405:in `_run_process_action_callbacks' 
# /home/chopper/.rvm/gems/[email protected]/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/activesupport/lib/active_support/callbacks.rb:88:in `send' 
# /home/chopper/.rvm/gems/[email protected]/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/activesupport/lib/active_support/callbacks.rb:88:in `run_callbacks' 
# /home/chopper/.rvm/gems/[email protected]/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/actionpack/lib/abstract_controller/callbacks.rb:17:in `process_action' 
# /home/chopper/.rvm/gems/[email protected]/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/actionpack/lib/action_controller/metal/rescue.rb:8:in `process_action' 
# /home/chopper/.rvm/gems/[email protected]/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/actionpack/lib/abstract_controller/base.rb:113:in `process' 
# /home/chopper/.rvm/gems/[email protected]/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/actionpack/lib/abstract_controller/rendering.rb:39:in `sass_old_process' 
# /home/chopper/.rvm/gems/[email protected]/gems/haml-3.0.0.beta.3/lib/sass/plugin/rails.rb:26:in `process' 
# /home/chopper/.rvm/gems/[email protected]/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/actionpack/lib/action_controller/metal/testing.rb:12:in `process_with_new_base_test' 
# /home/chopper/.rvm/gems/[email protected]/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/actionpack/lib/action_controller/test_case.rb:390:in `process' 
# /home/chopper/.rvm/gems/[email protected]/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/[email protected]/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/activesupport/lib/active_support/dependencies.rb:209:in `inject' 

任何人可以帮助我明白是怎么回事?这似乎是一个RSpec问题,因为控制器操作确实有效,但我可能是错的。

+0

'Factory'做了什么?这里的一切都很好。如果你不使用'Factory',但是为了测试而手动创建用户呢?你有没有将YAML装置加载到某处?当我发现我在夹具中调用受保护的方法时,我只见过这个错误。 – x1a4 2010-05-31 05:37:02

+0

工厂 是Factory_Girl的主要方法,但即使我只是使用Active Record来创建@user对象和@task对象没有任何变化。 – TheDelChop 2010-05-31 12:24:32

+0

我有类似的问题,没有线索,你解决了吗?我最终存根查找方法,但你做了同样的,它没有奏效。 – Mauricio 2010-09-22 21:51:49

回答

0

我使用您提供的信息重新创建了您的项目,并且控制器规格运行时没有任何失败(Rails 2.3.5,Ruby 1.8.7)。所以我猜测你的项目和/或Rails配置有什么不妥之处?对不起,我知道这并不多。

0

我最近得到这个错误消息与我的Rails 3应用程序使用rspec-rails 2.0.0.beta22(在Ruby 1.9.2下)。大量的头发拉动后,我升级到最近发布的rspec-rails 2.0.0.rc,并且错误消失了。所以,我建议升级你的rspec-rails gem。