简体   繁体   English

RSpec测试未能寻找新的眼光

[英]RSpec test failing looking for a new set of eyes

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 这个简单的routes.rb文件

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_active_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. 由于控制器操作实际上起作用,所以这似乎是RSpec问题,但是我可能错了。

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). 我使用提供的信息从头开始重新创建了您的项目,并且控制器规范运行无任何故障(Rails 2.3.5,Ruby 1.8.7)。 So I'm guessing there's something amiss in your project and/or Rails configuration? 因此,我猜测您的项目和/或Rails配置中存在某些问题吗? 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). 我最近在我的Rails 3应用程序中使用rspec-rails 2.0.0.beta22(在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. 经过大量的努力,我升级到了最近发布的rspec-rails 2.0.0.rc,错误消失了。 So, I'd recommend upgrading your rspec-rails gem. 因此,我建议您升级rspec-rails gem。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM