RSpec 异常
RSpec Exception
我正在创建一个 RSpec 测试来验证从网页检索到的数据。我调用 URL 到网页,然后验证返回的数据是否正确。但是,部分测试涉及重置后端,因此,服务器无法访问的时间很短。我想要一个 RSpec 测试,期望返回的数据是预期的内容。但是,在服务器重置的一小段时间里,我收到一个连接被拒绝的异常,这个异常导致整个测试失败。我想以某种方式执行 RSpec 期望并忽略抛出的所有异常。例如(伪代码):
Thread.new{"reset server code"}
wait_until{expect("retrieve server url data code").to eq("expected data").and ignore all exceptions()}
在重置周期期间服务器将处于特定状态,因此,我启动一个线程并等待在该状态期间返回预期数据。这就是为什么我不等待重置完成然后点击 URL 的原因。任何关于如何对某事执行期望并忽略所有异常的想法都会很棒。我不使用 .to raise_error 因为由于计时问题我不确定在我 wait_until 期间我会收到多少次异常(注意,这是一个包装器来执行代码直到期望已完成)。我 理想情况下 只想等到返回的数据正确,并忽略该过程中的所有异常。
也许最简单的方法是 rescue
从连接错误和 retry
:
begin
expect("retrieve server url data code").to eq("expected data")
rescue SomeConnectionError
sleep 1
retry
end
retry
关键字已记录 here。
You may retry rescued exceptions:
begin
# ...
rescue
# do something that may change the result of the begin block
retry
end
Execution will resume at the start of the begin block, so be careful
not to create an infinite loop.
我正在创建一个 RSpec 测试来验证从网页检索到的数据。我调用 URL 到网页,然后验证返回的数据是否正确。但是,部分测试涉及重置后端,因此,服务器无法访问的时间很短。我想要一个 RSpec 测试,期望返回的数据是预期的内容。但是,在服务器重置的一小段时间里,我收到一个连接被拒绝的异常,这个异常导致整个测试失败。我想以某种方式执行 RSpec 期望并忽略抛出的所有异常。例如(伪代码):
Thread.new{"reset server code"}
wait_until{expect("retrieve server url data code").to eq("expected data").and ignore all exceptions()}
在重置周期期间服务器将处于特定状态,因此,我启动一个线程并等待在该状态期间返回预期数据。这就是为什么我不等待重置完成然后点击 URL 的原因。任何关于如何对某事执行期望并忽略所有异常的想法都会很棒。我不使用 .to raise_error 因为由于计时问题我不确定在我 wait_until 期间我会收到多少次异常(注意,这是一个包装器来执行代码直到期望已完成)。我 理想情况下 只想等到返回的数据正确,并忽略该过程中的所有异常。
也许最简单的方法是 rescue
从连接错误和 retry
:
begin
expect("retrieve server url data code").to eq("expected data")
rescue SomeConnectionError
sleep 1
retry
end
retry
关键字已记录 here。
You may retry rescued exceptions:
begin # ... rescue # do something that may change the result of the begin block retry end
Execution will resume at the start of the begin block, so be careful not to create an infinite loop.