1

平台:Ubuntu 12.10

在我的终端中,我正在尝试运行bundle exec rspec spec/models/user_spec.rb

我正在关注 Michael Hartl 的 Learn Web Development:Ruby on Rails 教程来测试用户。

我的代码如下:

class User < ActiveRecord::Base      
    attr_accessible :email, :name  
end

我的错误如下所示:

root@NIyi-PC:/usr/sample_app# bundle exec rspec spec/models/user_spec.rb 
Rack::File headers parameter replaces cache_control after Rack 1.5.
/usr/sample_app/app/models/user.rb:13:in `<top (required)>': superclass mismatch for class User (TypeError)
4

1 回答 1

5

如果您已经class User在代码中首先声明了其他地方,则会发生此错误:

1.9.3-p374 :001 > class Bar; end
 => nil
1.9.3-p374 :002 > class Foo; end # first declaration, no superclass
 => nil
1.9.3-p374 :003 > class Foo < Bar; end # attempting to declare superclass later
TypeError: superclass mismatch for class Foo
    from (irb):3
    from /Users/Mark/.rvm/rubies/ruby-1.9.3-p374/bin/irb:16:in `<main>'

对比

1.9.3-p374 :001 > class Bar; end
 => nil
1.9.3-p374 :002 > class Foo < Bar; end # first declaration, including superclass
 => nil
1.9.3-p374 :003 > class Foo; end # don't have to mention the superclass later
 => nil

有时这可能很难追踪,但一个简单的起点是在整个项目中搜索“类用户”。

于 2013-03-21T14:53:21.043 回答