确保:创建FactoryGirl通过控制器

时间:2013-01-13 13:16:09

标签: ruby-on-rails-3 rspec2 factory-bot

由于数据库中已保存的数据,我的某些测试无效。我发现(下面的代码),FactoryGirl的create函数没有经过控制器步骤,以确保传递控制器中定义的所有内容。我的控制器做的一件事是在保存到数据库之前格式化一个属性,以确保保存字符串而不是数组,例如“星期一,星期二”而不是[“星期一,星期二”]。

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

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

1 个答案:

答案 0 :(得分: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