2016-08-02 16 views
0

Ich habe ein Formular-Objekt, das wie folgt aussieht:Wie testen, dass ein Formularobjekt eine Formulareigenschaft enthält?

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 

Wie schreibe ich eine Feature-Spezifikation zu testen, ob die AddressForm vorhanden ist?

Ich habe bereits eine Arbeits spec die anderen "Eigenschaften" zu testen (Titel, Geschlecht, etc.)

ich so etwas wie

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

Der Ausgang davon

ist versucht haben
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}, 

Das scheint mir wie es ist fast da, aber nicht ganz.

Ich bin sehr neu zu RSpec im Allgemeinen so leid, wenn ich nicht genug Informationen zur Verfügung gestellt habe.

Antwort

1

Ich glaube, Sie suchen be_a:

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

RSpec bildet einen unbekannten Matcher mit be_ beginnen, wie be_foo, ein Verfahren wie is_foo?. Sie könnten auch (weniger schön) schreiben

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