0

我有一个 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)>'
4

1 回答 1

1

听起来您有一个名为 Topic 的模块或命名空间,它首先被加载,因此在您的测试中,Topic 并不是指您的类。是否还有其他包含主题的文件,甚至是类 Topic::Question 或类似的文件?如果是这样,请尝试将其删除或明确说明。例如,改变:

class Topic::Question < ActiveRecord::Base

class Topic
  class Question < ActiveRecord::Base
于 2013-05-21T18:01:29.313 回答