【问题标题】:Ensure that :create of FactoryGirl goes through controller确保 FactoryGirl 的 :create 通过控制器
【发布时间】:2013-01-13 13:16:09
【问题描述】:

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

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

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

【问题讨论】:

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


    【解决方案1】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-15
      • 1970-01-01
      相关资源
      最近更新 更多