0

我想我知道问题出在哪里,但我似乎无法弄清楚如何解决它。

这是我的models

用户

class User < ActiveRecord::Base
  attr_accessor :password
  attr_accessible :name, :email, :password, :password_confirmation

  has_many :student_groups
...
end  

学生组

class StudentGroup < ActiveRecord::Base
  attr_accessible :name

  belongs_to :user
  has_many   :subjects
  has_many   :students

end

主题

class Subject < ActiveRecord::Base
  attr_accessible :end_date, :name

  belongs_to :student_group
  belongs_to :student

end

学生

class Student < ActiveRecord::Base
  attr_accessible :gender, :name

  belongs_to :student_group
  has_many :subjects

end

在我的Student_Spec.rb我有以下测试编辑

...

before(:each) do
  @user = Factory(:user)
  @student_group_attr = { name: "4a"}
  @student_group = @user.student_groups.create(@student_group_attr)
  @date = Date.today+180
  @subject_attr = { name: "English", end_date: @date}
end

...

describe "Student associations" do

  before(:each) do
    @subject = @student_group.subjects.create!(@subject_attr)
    @student_attr = { name: "Example Student", gender: "Female" }
    @student = @student_group.students.create(@student_attr) 
  end

  it "should have the right associated student" do
    @subject.student_id.should == @student.id
    @subject.student.should == @student
  end
end 

我在其他规格中进行了相同的测试并且效果很好 - 我在控制台中检查了它并得到了这个:

2.0.0-p0 :015 > @subject
=> #<Subject id: 1, name: "English", student_group_id: 1, student_id: nil, end_date: "2013-11-18", created_at: "2013-05-22 15:08:44", updated_at: "2013-05-22 15:08:44">

因此,无论出于何种原因,student_id 都没有与主题相关联……我在这里做错了什么?谢谢!

4

2 回答 2

0

重新加载@subject,可能它没有从数据库加载,因此它是空的

于 2013-05-22T16:22:46.437 回答
0

弄清楚了。

将模型更改为以下内容:

student_groups.rb

class StudentGroup < ActiveRecord::Base
  attr_accessible :name

  belongs_to :user
  has_many   :students

end

学生.rb

class Student < ActiveRecord::Base
  attr_accessible :gender, :name

  belongs_to :student_group
  has_many :subjects

end

主题.rb

class Subject < ActiveRecord::Base
  attr_accessible :end_date, :name

  belongs_to :student

end

型号规格如下:

student_group_spec.rb

require 'spec_helper'

describe StudentGroup do

  before(:each) do
    association_attr  
  end

  it "should create a new instance with valid attributes" do
    @user.student_groups.create!(@attr).should be_valid
  end

  describe "User associations" do

    it "should have a user attribute" do
      @student_group.should respond_to(:user)
    end

    it "should have the right associated user" do
      @student_group.user_id.should == @user.id
      @student_group.user.should == @user
    end

  end

  describe "Student associations" do

    it "should have a student attritube" do
      @student_group.should respond_to(:students)
    end

  end

end

student_spec.rb

require 'spec_helper'

describe Student do

  before(:each) do
    association_attr
  end

  it "should create a new instance with valid attributes" do
    @student_group.students.create!(@attr).should be_valid
  end

  describe "Student_Group associations" do

    it "should have a student_group attribute" do
      @student.should respond_to(:student_group)
    end

    it "should have the right associated student_group" do
      @student.student_group_id.should == @student_group.id
      @student.student_group.should == @student_group
    end

  end

  describe "Subject associations" do

    it "should have a subject attribute" do
      @student.should respond_to(:subjects)
    end

  end

end

subject_spec.rb

require 'spec_helper'

describe Subject do

  before(:each) do
    association_attr  
  end

  it "should create a new instance with valid attributes" do
    @student.subjects.create!(@subject_attr).should be_valid
  end

  describe "Student associations" do

    it "should have a student attribute" do
      @subject.should respond_to(:student)
    end

    it "should have the right associated student" do
      @subject.student_id.should == @student.id
      @subject.student.should == @student
    end 

  end

end

最后更改了spec_helper.rb如下:

def association_attr
  # User attritbutes 
  @user = Factory(:user)

  # Student_group
  @student_group = @user.student_groups.create(@student_group_attr)
  # Student_group attributes
  @student_group_attr = { name: "4a"}

  # Student 
  @student = @student_group.students.create(@student_attr)
  # Student attributes
  @student_attr = { name: "Example Student", gender: "Transgender" }

  # Subject
  @subject = @student.subjects.create!(@subject_attr)
  # Subject attributes
  @subject_attr = { name: "English", end_date: @date}
  @date = Date.today+180
end

感谢Frederick CheungBilly Chan的评论。

于 2013-05-22T18:42:41.750 回答