Guest User

Untitled

a guest
Jul 23rd, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. # Using hashes in RSpec to check different variable/value combinations
  2. # Often in RSpec, you want to check all different combinations of values for a number of variables, such as in the following example:
  3.  
  4. RSpec.describe RealStateWorker do
  5. context "when active is true and rented is true" do
  6. it { expect(something).to eq(something) }
  7. end
  8.  
  9. context "when active is true and rented is false" do
  10. it { expect(something).to eq(something) }
  11. end
  12.  
  13. context "when active is false and rented is true" do
  14. it { expect(something).to eq(something) }
  15. end
  16.  
  17. context "when active is false and rented is false" do
  18. it { expect(something).to eq(something) }
  19. end
  20. end
  21.  
  22.  
  23. # Because each variable has two possible values, there are four combinations to test. But, what if we had more variables? This can very quickly grow out of hand. Using hashes is a concise way of managing this:
  24.  
  25. RSpec.describe RealStateWorker do
  26. scenarios = [
  27. { active: true, rented: true, expected_result: :something },
  28. { active: true, rented: true, expected_result: :something_else },
  29. { active: true, rented: false, expected_result: :something },
  30. { active: true, rented: false, expected_result: :something_else },
  31. { active: false, rented: true, expected_result: :something },
  32. { active: false, rented: true, expected_result: :something_else },
  33. { active: false, rented: false, expected_result: :something },
  34. { active: false, rented: false, expected_result: :something_else }
  35. ]
  36.  
  37. scenarios.each do |scenario|
  38. context "when active is #{scenario[:active]} and rented is #{scenario[:rented]}" do
  39. it "is #{expected_result}" do
  40. expect(something).to eq(scenarios[:expected_result])
  41. end
  42. end
  43. end
  44. end
Add Comment
Please, Sign In to add comment