1

您可以在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

4

1 回答 1

4

根据bematchers文档,如果为(不是或) ,expect(obj).to be则此测试通过。objnilfalse

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

这是一个演示示例:

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
于 2015-03-24T17:11:21.667 回答