在 rspec 中,以 'to be' 结尾的测试是什么意思?

In rspec what does a test that ends with 'to be' mean?

您可以在 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-期望值 3.0.4

根据 be matchers 文档,expect(obj).to be 如果 objtruthy(不是 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