1

我有一个模型答案:

class Answer < ActiveRecord::Base
  attr_accessible :content

  belongs_to :question
end

在我的问题模型中,我定义了has_many :answers. 我的答案模型有三列:内容(文本)、问题 ID(整数)、正确(布尔)。correct列的默认值为false

我创建了工厂来创建 Answer 对象:

factory :answer do
  content "Content of an answer"
  question

  factory :accept_answer do
    correct true
  end
end

在我的 rspec 文件中,我使用以下代码成功创建了新的答案对象:

let(:answer) { FactoryGirl.create(:answer, question: question) }

subject { answer }
its(:correct) { should be_false }

但是当我使用下面的代码创建 accept_answer 对象时:

describe "an accepted answer" do
  let(:accept_answer) { FactoryGirl.create(:accept_answer, question: question) } 
  it { accept_answer.correct.should be_true }
end

它有错误

Failure/Error: let(:accept_answer) { FactoryGirl.create(:accept_answer, question: question) }
ArgumentError:
   Factory not registered: accept_answer

我不知道我的代码有什么问题:(

4

1 回答 1

3

问题是因为 spork 服务器无法重新确定我所做的更改,所以我重新启动了 spork 服务器并且它可以工作。

于 2012-11-06T10:07:21.743 回答