这是捆绑exec黄瓜运行的输出。在黄瓜运行期间你可以看到@timeout_exception是nil,但它在运行时设置得很好。
And the output should be 'Execution Timeout Error: This deployment has taken too long to run' # features/step_definitions/my_steps.rb:309
expected: "Execution Timeout Error: This deployment has taken too long to run"
got: nil
(compared using ==)
(RSpec::Expectations::ExpectationNotMetError)
./features/step_definitions/my_steps.rb:310:in `/^the output should be '(.*)'$/'
features/timeout_lengthy_deploys.feature:25:in `And the output should be 'Execution Timeout Error: This deployment has taken too long to run''
Failing Scenarios:
cucumber features/timeout_lengthy_deploys.feature:11 # Scenario: Normal deploy that times out because it takes too long
这是我的测试,但即使@timeout_exception在代码运行期间有效,它在测试期间也是空的。那么如何测试是否设置了这个变量呢?
Then(/^the output should be '(.*)'$/) do |expectedException|
expect(@timeout_exception).to eq(expectedException)
end
这是Ruby代码。
log.info "Executing '#{command.join(' ')}'"
begin
timeout(config['deploy-timeout'].to_i) do
execute_and_log command
end
rescue Timeout::Error => e
@timeout_exception = "Execution Timeout Error"
log.error "#{@timeout_exception}"
raise e
end
Log.error打印出很好的“执行超时错误”但在期望期间为空(@timeout_exception)......
答案 0 :(得分:1)
测试期间不一定是空的,但在测试中无法访问。你正在做什么"测试"是属于测试对象的实例变量,而不是原始实例变量。
您没有说明实例变量所属的对象,但您可以使用...检索测试中的实例变量。
foo.instance_variable_get(:bar)
但是如果您正在测试实例变量,那么您的测试需要对应用程序的内部结构了解太多。更好的是你可以测试使用/输出实例变量的任何方法。