0

由于我的数据库中保存了数据,我的一些测试不起作用。我发现(下面的代码)FactoryGirl 的创建函数没有经过控制器步骤来确保传递控制器中定义的所有内容。我的控制器所做的一件事是在保存到数据库之前格式化属性以确保保存的是字符串而不是数组,例如“Monday,Tuesday”而不是 [“Monday,Tuesday”]。

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

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

4

1 回答 1

0

通过输入以下行解决了这个问题:

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
于 2013-01-13T15:00:53.843 回答