I have a complex condition to assert in one of my methods:
Today it's:
if deal.video_url.present? && deal.video_url_changed?
# do stuff
# about 15 lines of code
end
Unfortunately story_step.st_video_url_changed?
is giving me false positives and negatives in Rspec tests (see the issue also mentioned here: How can I test that no attribute on a model has been modified using rspec?)
Anyway, what I wish to do is modify this condition check line to become
"if we're on rails development or production environements, please check A(deal.video_url.present?) && B(deal.video_url_changed? ) but if Rails environement is Test, ONLY check A deal.video_url.present?
Of course I could write:
if Rails.env.development? || Rails.env.production?
if deal.video_url.present? && deal.video_url_changed?
# do stuff
# about 15 lines of code
end
else # test environment
if deal.video_url.present?
# do stuff
# about 15 lines of code
end
But, I feel it's really not DRY to repeat twice the exact same 15 lines of code to be executed when the condition pass.
I could factorize by creating a new method
if Rails.env.development? || Rails.env.production?
if deal.video_url.present? && deal.video_url_changed?
new_method_to_factorize
end
else # test environment
if deal.video_url.present?
new_method_to_factorize
end
end
def new_method_to_factorize
# do stuff
# about 15 lines of code
end
But I wonder is it possible to write this more cleanly and without creating a new method?
I tried but failed with the code below
if deal.video_url.present? && (deal.video_url_changed? unless Rails.env.test?)
# do stuff
# about 15 lines of code
end
EDIT
After many comments, I realized that i am trying to put crutches while I should deal with the very core of the bug. this bug was created because I found a hacky way to associate Deals and Steps. To solve the original issue, I created a new Stackoverflow question: Rails 4.2 / Rspec / rspec-retry - association belong/has_many failing