Rspec 基于条件的测试
Rspec tests based on condition
我有一个布尔变量condition
。我有一些 rspec 测试用例来检查是否存在输入字段。
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
但是所有的测试用例都被执行了。我尝试使用 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
如果要对语法或初始化局部变量进行任何更改,请告诉我 condition
。
您可以使用 if:
关键字,如 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
我有一个布尔变量condition
。我有一些 rspec 测试用例来检查是否存在输入字段。
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
但是所有的测试用例都被执行了。我尝试使用 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
如果要对语法或初始化局部变量进行任何更改,请告诉我 condition
。
您可以使用 if:
关键字,如 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