Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Using hashes in RSpec to check different variable/value combinations
- # Often in RSpec, you want to check all different combinations of values for a number of variables, such as in the following example:
- RSpec.describe RealStateWorker do
- context "when active is true and rented is true" do
- it { expect(something).to eq(something) }
- end
- context "when active is true and rented is false" do
- it { expect(something).to eq(something) }
- end
- context "when active is false and rented is true" do
- it { expect(something).to eq(something) }
- end
- context "when active is false and rented is false" do
- it { expect(something).to eq(something) }
- end
- end
- # 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:
- RSpec.describe RealStateWorker do
- scenarios = [
- { active: true, rented: true, expected_result: :something },
- { active: true, rented: true, expected_result: :something_else },
- { active: true, rented: false, expected_result: :something },
- { active: true, rented: false, expected_result: :something_else },
- { active: false, rented: true, expected_result: :something },
- { active: false, rented: true, expected_result: :something_else },
- { active: false, rented: false, expected_result: :something },
- { active: false, rented: false, expected_result: :something_else }
- ]
- scenarios.each do |scenario|
- context "when active is #{scenario[:active]} and rented is #{scenario[:rented]}" do
- it "is #{expected_result}" do
- expect(something).to eq(scenarios[:expected_result])
- end
- end
- end
- end
Add Comment
Please, Sign In to add comment