Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails rspec中的Before块在每次测试之前运行,而该块只应运行一次_Ruby On Rails_Ruby_Rspec - Fatal编程技术网

Ruby on rails rspec中的Before块在每次测试之前运行,而该块只应运行一次

Ruby on rails rspec中的Before块在每次测试之前运行,而该块只应运行一次,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,在测试开始时,我在do之前运行一个。我希望在运行测试之前执行一次before。但是,在每次测试之前,它正在运行: context "worker has failed first time and has generated only part of licenses" do before do order.already_went_through_license_worker = true order.save! order.pre_

在测试开始时,我在do之前运行一个
。我希望在运行测试之前执行一次
before
。但是,在每次测试之前,它正在运行:

context "worker has failed first time and has generated only part of licenses" do
      before do
        order.already_went_through_license_worker = true
        order.save!
        order.pre_calculate_licenses
        partial_generate_from_bundles(order.expected_license_bundles)
        LicenseService.new.create_from_order(order.id)
      end
      let!(:licenses){ License.where(order_id: order.id)}
      specify { subject.count.should == 34 }
      specify { subject.pluck(:is_from_offer_special).count(true).should == 4}
      specify { subject.pluck(:is_from_offer_special).count(false).should == 30 }
      specify { subject.pluck("license_offer.offer_original_id").compact.map(&:values).flatten.count(offer.id).should == 30}
      specify { subject.pluck("license_offer_special.offer_special_original_id").compact.map(&:values).flatten.count(offer_special_cyclic.id).should == 3}
      specify { subject.pluck("license_offer_special.offer_special_original_id").compact.map(&:values).flatten.count(offer_special_non_cyclic.id).should == 1 }
    end
当我将其更改为before(:all)时,我得到一个错误:

 let declaration `order` accessed in a `before(:context)` hook at:

如何使before块只运行一次。

如果没有其他帮助,可以使用one
指定
块方法:

specify do 
 subject.count.should == 34
 subject.pluck(:is_from_offer_special).count(true).should == 4
 #...
end
但这有一个缺点:如果一个期望失败,那么它之后的期望将无法运行。但如果你愿意,你可以解决这个问题

我怎样才能使before块只运行一次

在(:all)
之前,您已经这样做了

当我在(:all)
之前将其更改为
时,我得到一个错误:

这是另一个完全不相关的错误。你的block只运行一次,这是你要求的。这里的问题是上下文级块试图访问示例级声明。有点像试图从类方法访问实例变量:这些变量还不存在

一种可能的方法是内联所有依赖项(不要使用
let
,直接将代码放入块中):


什么是
order
,因为我没有看到
order
的声明。我有一种感觉,这是
let(:order){order.new(#…)}
,这是您在(:all)
之前使用
时的实际错误,因为它会在已记忆的助手
let
和(:all)
之前的上下文设置
之间创建上下文转换,您是否也可以粘贴它?啊,的确虽然不完全是被问到的问题,但这是一个更好的解决方案。对于聚合_失败,使用昂贵的设置进行单行测试真的没有多大意义。我自己一直在使用它。在某些时候,你也会意识到所有这些设置时间加起来了,所以你想把所有这些期望结合起来,为一个设置来修剪一些规格时间-这也很有用。
specify aggregate_failures: true do 
 subject.count.should == 34
 subject.pluck(:is_from_offer_special).count(true).should == 4
 #...
end
before :all do
  order = create :order # or whatever
  order.already_went_through_license_worker = true
  order.save!
  ...
end