2016-08-02 8 views
0

私はこのようになりますフォームオブジェクトがあります:私はAddressFormが存在するかどうかをテストする機能の仕様を書くにはどうすればよいフォームオブジェクトにフォームプロパティが含まれていることをテストするにはどうすればよいですか?

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 

を?

私はすでに他の「プロパティ」をテストするための作業スペック私は

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の新機能は一般的に新しく、十分な情報を提供していないとすみませます。

答えて

1

私はあなたがbe_aを探していると思う:

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

RSpecのはis_foo?のような方法に、be_fooのように、be_で始まる未知のマッチャーをマッピングします。あなたも書くことができます(それほどうまく書けません)

it 'must have the address form present' do 
    expect(form.address.is_a? AddressForm).to be true 
end 
関連する問題