我的 factory.rb 文件中定义了以下工厂:
require 'factory_girl'
FactoryGirl.define do
sequence(:email) {|n| "person-#{n}@example.com" }
factory :country do
...
end
factory :state do
country
...
end
factory :school do
name "Test School"
country
state
end
factory :user do
school
email
...
end
end
当在 rspec 中调用FactoryGirl.create(:school)
我的描述符之一进行测试时,会创建两所名为“Test School”的学校。
我认为 factory.rb 中定义的工厂只是一堆未保存的实例对象,有人可以澄清我为什么会遇到这个问题吗?
这是确切的rspec:
require 'spec_helper'
describe "school login" do
it "displays a success message upon successful login to school",do
school = FactoryGirl.create(:school)
user = FactoryGirl.create(:user, :username => "jdoe")
School.all.each do |school|
puts school.name #2x => "Test School"
end
visit school_path(user.school)
click_link('login')
fill_in "username", :with => "jdoe"
fill_in "password", :with => "secret"
click_button "Sign in"
expect(page).to have_selector(".alert-success")
end
end