确保:创建FactoryGirl通过控制器



我的一些测试不工作,因为我的数据库中保存了数据。我发现(下面的代码)FactoryGirl的create函数没有通过控制器步骤来确保传递控制器中定义的所有内容。我的控制器做的一件事是格式的属性保存到数据库之前,以确保保存字符串,而不是一个数组,例如。用"Monday,Tuesday"代替["Monday,Tuesday"]。

if Schedule.count == 0
  FactoryGirl.create(:schedule) 
end

如何设置before语句使create功能执行控制器步骤?

通过添加以下行来解决这个问题:

before { FactoryGirl.create(:default_for_overlap_schedule) }

完整的测试是:

describe "has schedule overlap" do
    before { FactoryGirl.create(:default_for_overlap_schedule) }
    let!(:overlap_schedule) { FactoryGirl.attributes_for(:overlap_schedule) }
    it "prompts user of error" do
        expect{ post :create, schedule: overlap_schedule  }.not_to change(Schedule, :count).by(1)
        flash[:error].should eq("Schedule has an overlap")
    end
end

最新更新