在rspec中,以“将来”结束的测试是什么意思?

时间:2015-03-24 17:09:14

标签: ruby rspec rspec-expectations

你可以在这里找到一个例子 https://github.com/cloudfoundry/cloud_controller_ng/blob/c63d33c0b1c2298d49a0bad959222b9c3daba16a/spec/unit/controllers/services/service_instances_controller_spec.rb#L1748

本块中的第二个测试显示了这一点:

expect(last_response).to have_status_code 202
expect(decoded_response['entity']['guid']).to be
expect(decoded_response['entity']['status']).to eq 'queued'

我看到我们正在匹配Matchers::BuiltIn::Be的新实例, 但是在这一点上很难看出我们实际匹配的是什么。

Ruby 2.1.3,rspec 3.0.0,rspec-expectations 3.0.4

1 个答案:

答案 0 :(得分:4)

根据be matchers文档expect(obj).to be,如果obj truthy (不是nilfalse),则此测试通过

expect(decoded_response['entity']['guid']).to be表示如文档所述,如果decoded_response['entity']['guid']的值是任何对象,而不是nilfalse,则测试将通过。

以下是演示示例:

RSpec.describe "be matcher" do
  context "when object is truthy" do
    specify { expect(2).to be }
  end
  context "when object is not truthy" do
    specify { expect(nil).not_to be }
  end
end

让我们运行这个测试: -

[arup@Ruby]$ rspec --format d spec/a_spec.rb

be matcher
  when object is truthy
    should be
  when object is not truthy
    should not be

Finished in 0.00254 seconds (files took 0.42175 seconds to load)
2 examples, 0 failures