0

我有一个看起来像这样的表单对象:

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存在?

我已经有一个工作规范来测试其他“属性”(标题、性别等)

我试过类似的东西

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 很陌生,如果我没有提供足够的信息,我很抱歉。

4

1 回答 1

1

我想你正在寻找be_a

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

be_RSpec 将一个以like开头的未知匹配器映射be_foo到类似的方法is_foo?。你也可以写(不太好)

it 'must have the address form present' do
  expect(form.address.is_a? AddressForm).to be true
end
于 2016-08-03T13:00:32.817 回答