Ruby on rails sRails 4.2/Rspec/Rspec重试-关联属于/有许多失败
我在rspec上面临着一个巨大的问题,每次我试图解决一个bug,它都会产生另一个bug 我有一个有很多步骤的模型交易 型号/步骤.rbRuby on rails sRails 4.2/Rspec/Rspec重试-关联属于/有许多失败,ruby-on-rails,ruby,ruby-on-rails-4,rspec,Ruby On Rails,Ruby,Ruby On Rails 4,Rspec,我在rspec上面临着一个巨大的问题,每次我试图解决一个bug,它都会产生另一个bug 我有一个有很多步骤的模型交易 型号/步骤.rb belongs_to :deal, :foreign_key => 'deal_id' has_many :steps, dependent: :destroy do # added to enable maximum nb of steps a deal can have # to t
belongs_to :deal, :foreign_key => 'deal_id'
has_many :steps, dependent: :destroy do
# added to enable maximum nb of steps a deal can have
# to take into account in active admin
# in order to calculate the correct new nb of step to compare to the authorized limit
# source: homeonrails.com/2012/10/validating-nested-associations-in-rails/
def length
reject(&:marked_for_destruction?).length
end
end
model/deal.rb
belongs_to :deal, :foreign_key => 'deal_id'
has_many :steps, dependent: :destroy do
# added to enable maximum nb of steps a deal can have
# to take into account in active admin
# in order to calculate the correct new nb of step to compare to the authorized limit
# source: homeonrails.com/2012/10/validating-nested-associations-in-rails/
def length
reject(&:marked_for_destruction?).length
end
end
我所有bug的要点是如何使我的一个功能rspec测试工作,在这里我将交易和步骤关联起来。我曾经使用工厂女孩“普通”瞬态,这会更干净,但我不得不离开它(见下文),因为我们有特殊要求:
factory :deal_with_associated_steps do
to_create {|instance| instance.save(validate: false) } # skip validate
transient do
steps_count 27
end
after(:create) do |deal, evaluator|
create_list(:steps, evaluator.steps_count, deal: deal)
end
end
我之所以不再使用这种“暂时”技术来创建与交易相关的多个步骤,是因为我们的应用程序非常特殊
当您为给定交易执行所有相关步骤的st_外观\u顺序\u nb(整数)时,它必须始终为:
-从0开始
-然后没有间隙,所以1,2,3
在交易模型交易上的一些事前验证允许我确保这种情况始终如此。如果一个步骤的外观为1,而另一个步骤的外观为3,则无法处理相关步骤,但没有外观为2的步骤。您不能有外观为0的步骤。它必须是一个系列0,1,2,3…等等
这实际上仍然适用于在Factory Girl中创建步骤的“经典瞬态”方法。但是,上面创建步骤的经典“瞬态”方法的问题是,我有一个可以帮助我重新进行功能测试的方法,正如许多其他具有复杂UI/javascript页面的rspec用户一样,有时我的前端测试由于某些js/加载原因第一次失败,如果重复足够多,第二次或第三次就可以了。因此,我的大多数功能测试都运行了4次,有些只通过了第2次或第3次:)gem rspec重试很简单,但我创建与工厂女孩相关的步骤的“瞬态”方式存在一个非常严重的问题:
我之所以会出错,是因为如果测试在第一次“重试”时失败,第二次,就好像测试应用程序认为st_appearance_order_nb 0到4已经被执行了(实际上它们是由第一次rspec“try”创建的),所以它现在创建了4个新步骤,st_appearance_nb分别为5、6、7和8
然后。。。导致错误,因为我有一个before_验证,以确保交易关联步骤的st_外观_顺序_nb始终从0开始,然后逐个递增
因此,使用rspec retry,我不能使用Factory Girl创建关联步骤的临时方法,至少在我找到另一种方法时,这是我的结论:我决定以这种方式“手动”创建关联步骤
let!(:deal_with_videos) { create(:deal,
title: "title deal 1" ) }
video_urls = [ "", # no video allowed on first step
"https://www.facebook.com/418234631906607/videos/495053617558041", # square video (5 sec)
"https://www.facebook.com/pili.morillo.56/videos/352355988613922", # landscape video with internal black sidebars
"https://www.facebook.com/rihanna/videos/10155330511896676/", # landscape video no internal black sidebars
"https://www.facebook.com/videos/1592424637461205/", # portrait video
""
]
(0..5).each do |n|
let!(:"deal_with_videos_step#{n}") {
FactoryGirl.create(:step,
st_appearance_order_nb: n,
st_video_url: video_urls[n],
deal: deal_with_videos)
}
end
这修正了错误,我99%的测试都成功了,但现在这篇文章的问题是:我的一个测试失败了:因为非常奇怪的是,我将交易和步骤关联起来的方式不是完全有效,而是部分有效。。。让我补充一下,在生产和开发模式下,一切都很好
describe "on Deal Page load, the view behaves appropriately in terms of video" do
let(:action) { visit actual_deal_page_path(deal_with_videos) }
let(:fb_player_visibility) { "hidden" }
let(:video_href_set_by_app_js) { nil.to_s }
it_behaves_like "a view where the FB video player behaves appropriately"To be clear I found a very hack way to do stuff, but it created another issue in a ripple effect a new bug as the way I was doing it was making the test suite think there was no new
测试失败的原因我现在知道了:它失败,因为以下内容在验证之前从未执行过
models/deal.rb
before_validation :extract_st_embed_hostings_from_st_video_urls
def extract_st_embed_hostings_from_st_video_urls
puts "beacon1"
self.steps.reject(&:marked_for_destruction?).each do |step|
puts "beacon2"
# do stuff
end
end
我知道这一点,因为这是带有puts消息的测试环境中的问题,当我在测试块上运行rspec测试时,我只看到“beacon1”,而没有看到beacon2(在dev和prod中,两条消息都可以看到)
我想知道为什么它没有被执行
因此,我在测试中添加了一些put
,以了解为什么行self.steps.reject(&:标记为| u for | u destruction?)。每个do | step |
没有输出任何内容。我的交易和步骤的关联在测试中可能不起作用吗
describe "on Deal Page load, the iew behaves appropriately in terms of video" do
before do
action
puts deal_with_videos.steps.to_json
puts deal_with_videos.steps[1].to_json
puts deal_with_videos.id
puts deal_with_videos_step0.deal_id
puts deal_with_videos_step0.deal.title
puts deal_with_videos_step0.to_json
end
let(:action) { visit actual_deal_page_path(deal_with_videos) }
let(:fb_player_visibility) { "hidden" }
it_behaves_like "a view where the FB video player behaves appropriately"
end
结果很奇怪:
- “puts deal_with_videos.steps.to_json”给了我[]=>所以它们似乎没有关联
- “puts deal_with_videos.steps.to_json”给出的“null”与前面的
puts
- 但netx 2的推出带来了更多的困惑:
- “puts deal_with_videos.steps.to_json”给出的“null”与前面的
- 但最后一个“看跌期权”给我:
belongs_to :deal, :foreign_key => 'deal_id'
has_many :steps, dependent: :destroy do
# added to enable maximum nb of steps a deal can have
# to take into account in active admin
# in order to calculate the correct new nb of step to compare to the authorized limit
# source: homeonrails.com/2012/10/validating-nested-associations-in-rails/
def length
reject(&:marked_for_destruction?).length
end
end
- 创作