Ruby 小黄瓜测试中的空变量
这是bundle exec cumber运行的输出。您可以看到@timeout_异常在cucumber运行期间为nil,但在运行期间设置为罚款Ruby 小黄瓜测试中的空变量,ruby,Ruby,这是bundle exec cumber运行的输出。您可以看到@timeout_异常在cucumber运行期间为nil,但在运行期间设置为罚款 And the output should be 'Execution Timeout Error: This deployment has taken too long to run' # features/step_definitions/my_steps.rb:309 e
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\u异常在代码运行期间起作用,在测试期间也是空的。那么,如何测试是否设置了此变量
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很好地打印了“执行超时错误”,但在expect期间为空(@Timeout\u exception).它在测试期间不一定为空,但在测试中不可访问。您正在“测试”的是属于测试对象的实例变量,而不是原始实例变量 您没有说实例变量属于哪个对象,但是您可以使用
foo.instance_variable_get(:bar)
但是如果您正在测试实例变量,那么您的测试需要了解太多关于应用程序内部的信息。最好是测试使用/输出实例变量的任何方法