如何测试表单对象是否包含表单属性?

How to test that a form object includes a Form property?

我有一个如下所示的表单对象:

class LeaderForm < UserForm
  feature Reform::Form::MultiParameterAttributes

  property :title
  property :gender
  property :phone_number
  property :date_of_birth, multi_params: true

  property :address, form: AddressForm # need to test this line

  validates :title, :gender, :phone_number, :date_of_birth, presence: true
end

如何编写功能规范来测试 AddressForm 是否存在?

我已经有一个工作规范来测试另一个"properties"(头衔、性别等)

我试过

it 'must have the address form present' do
  expect(form.address).to include(AddressForm)
end

其输出为

  1) LeaderForm must have the address form present
       Failure/Error: expect(form.address).to include(AddressForm)

         expected #<AddressForm:0x007f89231c3280 @fields={"address1" => nil, "address2" => nil, "address3" => nil, "city" => ni...odel::Errors:0x007f89231c2b50 @base=#<AddressForm:0x007f89231c3280 ...>, @messages={}, @details={}>> to include AddressForm, but it does not respond to `include?`
         Diff:
         @@ -1,2 +1,41 @@
         -[AddressForm]
         +#<AddressForm:0x007f89231c3280
         + @_changes={},
         + @errors=
         +  #<Reform::Form::ActiveModel::Errors:0x007f89231c2b50
         +   @base=#<AddressForm:0x007f89231c3280 ...>,
         +   @details={},
         +   @messages={}>,
         + @fields=
         +  {"address1"=>nil,
         +   "address2"=>nil,
         +   "address3"=>nil,
         +   "city"=>nil,
         +   "postal_code"=>nil,
         +   "country"=>nil},

在我看来它几乎就在那里,但还不完全是。

总的来说,我对 RSpec 很陌生,如果我没有提供足够的信息,我深表歉意。

我想你在找 be_a:

it 'must have the address form present' do
  expect(form.address).to be_a(AddressForm)
end

RSpec 将以 be_ 开头的未知匹配器映射到 is_foo? 等方法。你也可以写(不太好)

it 'must have the address form present' do
  expect(form.address.is_a? AddressForm).to be true
end