I have a Boolean variable condition. I have some rspec test cases to check for the presence of an input field.
if(condition == true)
   execute the following test cases. 
   it "some test case"
   end
   it "some test case 2"
   end
if(condition == false)
   execute the following test cases. 
   it "some test case 3"
   end
   it "some test case 4"
   end
But all test cases are executed. I tried using context.
context "When condition is true"
  let(:condition) { TRUE }
  it "some test case"
  end
  it "some test case 2"
  end
context "When condition is false"
  let(:condition) { FALSE}
  it "some test case 3"
  end
  it "some test case 4"
  end
Please let me know if there any changes to be done either on the syntax or initializing the local variable condition.
You can use the if: keyword as documented in RSpec documentation
RSpec.describe "conditional contexts" do
  condition = true
  context "when true", if: condition do
    it 'passes' do
      expect(true).to be_truthy
    end
  end
  condition = false
  context "when false", if: !condition do
    it 'passes' do
      expect(false).to be_falsey
    end
  end
  condition = "non-nil"
  context "will not be run", if: condition.nil? do
    it 'will not get run' do
      expect(nil).to be_nil
    end
  end
end
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With