I am trying to figure out the way to properly implement tests (RSpec) for a method structured in the following way:
def foo(id)
url = "xxx#{id}"
ean = bar(url)
baz(ean)
end
bar(url)
and baz(ean)
are both resource-intensive, so my idea was to first test foo(id)
, and then to test bar(url)
and baz(ean)
only if foo(id)
fails.
it '...' do
caracteristics = foo(123)
expected_caracteristics = { a: 1, b: 2, c: 3 }
expect(caracteristics).to be_truthy
expect(caracteristics.size).to be >= 1
expect(caracteristics).to eq(expected_caracteristics)
end
# I want to run the following tests only if the previous one fails
it '...' do
ean = bar('test.com')
expect(ean).to include("XXX")
end
it '...' do
caracteristics = baz(ean)
expected_caracteristics = { a: 1, b: 2, c: 3 }
expect(caracteristics).to be_truthy
expect(caracteristics.size).to be >= 1
expect(caracteristics).to eq(expected_caracteristics)
end
Not only am I not sure about how to run a test only if another one fails, but I am also not sure whether this is the correct way of "structuring" this test.