繁体   English   中英

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

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

我有一个交易模型与模型步骤相关联。 交易has_many步骤和步骤属于交易。

我创建了一个Deal Factory:

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

由于与验证相关的特定原因以及gem rspec-retry的使用,我无法通过使用Factory Girl提供的“通常”after_create或Transients来创建关联的步骤,并且它没关系,因为我的技术到目前为止在我的所有测试中都有效。 她是我为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步骤属性,这些属性不同,不能只是放在上面的代码中,因为它们彼此完全不同,每次PLUS它们才开始出现在step1上(step0不能有一段录像)。 我需要发送数据的这些属性是:

  • VIDEO_URL

  • video_x

  • video_y

第一次尝试:

我尝试了下面的代码,但它失败并给我错误: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都是零,似乎我无法注入/更新这些属性。

是否有一种干净/正确的方法来更新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的前块,以便在测试示例之前运行它。 这就是为什么你得到错误' 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM