Rails 4.2 - 在创建 Factory Bot 记录后更新其多个属性

Rails 4.2 - Update multiple attributes of a Factory Bot record after it is created

我有一个关联了模型步骤的交易模型。一笔交易 has_many 个步骤,一个步骤属于一个交易。

我创建了一个交易工厂:

let!(:deal1)  { create(:deal, 
                                partner:          partner,                                
                                title:            "Deal1" ) }

由于与验证和使用 gem rspec-retry 相关的具体原因,我无法使用 "usual" after_create 或提供的瞬态来创建关联的步骤由 Factory Girl 编写,没关系,因为到目前为止,我的技术在我的所有测试中都有效。她是我为 Deal1 创建 5 个相关步骤的方法:

  (0..4).each do |n|
        let!(:"deal1_step#{n}") {
          FactoryGirl.create(:step,
            order_nb: n,
            message_content: "this is message #{n}",        
           background_asset_filename: "campaigns/assets_for_tests/backgroundimage#{n}.jpg",  
            deal: deal)
        }
      end

这会创建 deal1_step0、deal1_step1、deal1_step2、deal1_step3 和 deal1_step4,正如我所需要的,它今天在我的所有测试中都有效

我的问题来了,因为我现在必须向每个 deal1 步骤添加不同的属性,不能只放在上面的代码中,因为它们每次都完全不同,而且它们只在步骤 1 开始出现( step0 不能有视频)。我需要将数据发送到的这些属性是:

第一次尝试:

我尝试了下面的代码但它失败了并给我错误: deal1_step0.video_url = nil #step0上不能有任何视频(为此设置了验证)

`deal1_step1` is not available on an example group (e.g. a `describe` or `context` block). It is only available from within individual examples (e.g. `it` blocks) or from constructs that run in the scope of an example (e.g. `before`, `let`, etc).

deal1_step0.video_url = nil
deal1_step0.video_x = nil
deal1_step0.video_y = nil

deal1_step1.video_url = "https://www.facebook.com/rihanna/videos/10155221800656676/"
deal1_step1.video_x = 500
deal1_step1.video_y = 500

deal1_step2.video_url =  "https://www.facebook.com/ClaraLionelFoundation/videos/1821445664574948/"
deal1_step2.video_x = 500
deal1_step2.video_y = 300

deal1_step3.video_url  = "https://www.facebook.com/rihanna/videos/10155330511896676/"
deal1_step4.video_x = 250
deal1_step4.video_y = 500

第二次尝试:

我也试过类似的东西但得到了类似上面的错误: deal1_step1.update_attributes( video_url:

"https://www.facebook.com/rihanna/videos/10155221800656676/",
video_x: 500,
video_y: 500 )

第三次尝试:

然后我试着创建了一些符号:

  let(:video0) { nil }
  let(:video1) { "https://www.facebook.com/rihanna/videos/10155221800656676/" }
  let(:video2) { "https://www.facebook.com/ClaraLionelFoundation/videos/1821445664574948/" }
  let(:video3) { "https://www.facebook.com/rihanna/videos/10155330511896676/" }


(0..4).each do |n|
    let!(:"deal1_step#{n}") {
      FactoryGirl.create(:step,
        video_url: :"video#{n}",
        video_x: 500,
        video_y: 500,
        st_background_asset_filename: "campaigns/042017/assets_for_tests_and_dev/backgroundimage#{n}.jpg",  
        deal: deal1)
    }
  end

这次我在测试中没有出错 运行 但是它不起作用,因为视图认为所有这些 video_url 都是零,看来我没有管理inject/update 这些属性。

有没有clean/proper方法更新deal1_step0、deal1_step1、deal1_step2、deal1_step3和deal1_step4的属性?

最简单的解决方案是定义变化值的数组并在 FactoryGirl 步骤中使用它们

urls = [nil, "https://www.facebook.com/rihanna/videos/10155221800656676/", ...]
xs = [nil, 500, ...]
ys = [nil, 200, ...]
(0..4).each do |n|
  let!(:"deal1_step#{n}") {
    FactoryGirl.create(:step,
      video_url: urls[n],
      video_x: xs[n],
      video_y: ys[n],
      st_background_asset_filename: "campaigns/042017/assets_for_tests_and_dev/backgroundimage#{n}.jpg",  
      deal: deal1)
  }
end

对于其他任何事情,您都需要了解 let/let! 的工作原理。 let 在示例中定义了一个方法(以传入的符号命名)(它实际上是在一个混入的模块上,效果相同),它会记住传递给它的块的响应。 let!调用let然后定义一个调用方法let的before块定义所以它已经在运行之前测试示例了。这就是为什么您收到错误“deal1_step1 在示例组中不可用(例如...”,因为该方法是在示例中定义的,而不是在组中定义的。

因此,如果您不想像上面的数组示例那样执行操作,那么您需要在示例上下文中获取 运行 的块中进行属性更新,因此 before 块-喜欢

(0..4).each do |n|
    let!(:"deal1_step#{n}") {
      FactoryGirl.create(:step,
        order_nb: n,
        message_content: "this is message #{n}",        
       background_asset_filename: "campaigns/assets_for_tests/backgroundimage#{n}.jpg",  
        deal: deal)
    }
  end
before do 
  deal1_step1.video_url = "https://www.facebook.com/rihanna/videos/10155221800656676/"
  deal1_step1.video_x = 500
  deal1_step1.video_y = 500
  # may need to save deal1_step1 depending on exactly what you're looking for

  deal1_step2.update_attributes(video_url: "https://www.facebook.com/rihanna/videos/10155221800656676/", video_x: 500, video_y: 500 )

  ...
end