0

我有以下rspec:

require 'spec_helper'

describe KnowledgesController do

  before (:each) do
    @knowledge = FactoryGirl.create(:knowledge)
    @professor = FactoryGirl.create(:professor)
    sign_in @professor
  end

  describe "GET 'show'" do

    it "should be successful" do
      response.should be_success
    end

  end

end

执行时给我以下错误:

Failure/Error: @professor = FactoryGirl.create(:professor)
     ActiveRecord::RecordInvalid:
       translation missing: es.activerecord.errors.messages.record_invalid
     # ./spec/controllers/knowledge_controller_spec.rb:7:in `block (2 levels) in <top (required)>'

如果我改变这个:

before (:each) do
        @knowledge = FactoryGirl.create(:knowledge)
        @professor = FactoryGirl.create(:professor)
        sign_in @professor
      end

为了:

 before (:each) do
    @professor = FactoryGirl.create(:professor)
    @knowledge = FactoryGirl.create(:knowledge)
    sign_in @professor
 end

我收到此错误:

Failure/Error: @knowledge = FactoryGirl.create(:knowledge)
     ActiveRecord::RecordInvalid:
       translation missing: es.activerecord.errors.messages.record_invalid

所以,我不认为记录是无效的,因为在一种情况下它取决于知识,在另一种情况下取​​决于教授。

你们有什么想法会发生什么吗?

这些工厂是:

#Knowledge

FactoryGirl.define do
  factory :knowledge do |knowledge|
    knowledge.association(:professor)
    knowledge.association(:subject)
    knowledge.association(:level)
  end
end

#Professor

FactoryGirl.define do
    factory :professor do
      first_name 'Mister'
      last_name 'Professor'
      ...
    end
end
4

1 回答 1

1

知识类有一个相关的教授记录。由于您没有将明确的教授记录传递给 create 方法,因此您在创建知识的同时创建了一个新教授。

一种或另一种方式,您正在创建两个具有相同属性的教授。必须验证唯一性或类似的东西。

尝试这个:

before (:each) do
  @professor = FactoryGirl.create(:professor)
  @knowledge = FactoryGirl.create(:knowledge, :professor => @professor)
  sign_in @professor
end

它应该工作。

于 2012-06-28T23:54:21.047 回答