我有一个Rails 4 beta应用程序(在Ruby 2上),我收到一个我无法理解的错误。
我有一些失败的规格,因为我的模型类没有方法'create',即使我继承了ActiveRecord :: Base。错误消息是将我的类称为模块(undefined method 'create' for Topic:Module
),这似乎很奇怪。
规格/模型/ topic_spec.rb:
require "spec_helper"
describe Topic do
it "should create a new topic given valid attributes" do
Topic.create!({:created_by_id => 1, :title => "Test" })
end
end
应用/模型/ topic.rb
class Topic < ActiveRecord::Base
include ActiveModel::ForbiddenAttributesProtection
validates :title => :presence => ture
validates :created_by_id => :presence => true
end
错误讯息:
$ rspec spec/models/topic_spec.rb
F
Failures:
1) Topic should create a new topic given valid attributes
Failure/Error: Topic.create!({:created_by_id => 1, :title => "Test" })
NoMethodError:
undefined method `create' for Topic:Module
# ./spec/models/topic_spec.rrc:15:in `block (2 levels) in <top (required)>'
答案 0 :(得分:1)
听起来你有一个名为Topic的模块或名称空间首先被加载,因此在你的测试中,Topic并不是指你的类。是否有其他文件中包含主题,甚至类似主题::问题或类似的东西?如果是这样,请尝试将它们取出或明确说明。例如,更改:
class Topic::Question < ActiveRecord::Base
到
class Topic
class Question < ActiveRecord::Base