我在用户模型上有自我加入关系:
class User < ActiveRecord::Base
has_many :students, class_name: "User",
foreign_key: "teacher_id"
belongs_to :teacher, class_name: "User"
end
我有一名学生控制员:
class StudentsController < ApplicationController
def index
@students = current_user.students
end
end
索引方法的相应视图如下所示:
%ul#students-list
- @students.each do |student|
%li= student.full_name
正如您所料,当我导航到/students
时,我可以看到学生列表。耶!
然而,我在Capybara中编写的功能规范失败了。
require 'spec_helper'
describe "Navigating to students" do
let!(:teacher) { create(:teacher_user) }
let!(:student) { create(:student_user, teacher: teacher) }
before do
login_as teacher
visit '/students'
end
it "displays a list of students" do
expect(page).to have_selector('ul#students-list li')
end
end
这是我的工厂:
FactoryGirl.define do
factory :teacher_user, class: "User" do
email
password "asterisk"
first_name "James"
last_name "Baldwin"
association :user_type, factory: :user_type_teacher
end
factory :student_user, class: "User" do
email
password "asterisk"
first_name "Yuri"
last_name "Kochiyama"
association :user_type, factory: :user_type_student
end
end