2012-12-17 88 views
0

提交的模型形式出廠值我有一個需要以下字段的客戶端模式:如何通過水豚

validates :name, :address1, :city, :state, :country, :zipcode,
:contactname, :email, presence: true

創建一個新的客戶現在我書面方式測試。我創建FactoryGirl客戶對象,我該如何填寫表格出廠值,而無需手動編寫fill_in "field_id", with: factory.field_value用於各個領域,像我已經做了,如下圖所示的代碼。

describe "with valid information" do 
    let(:client) { FactoryGirl.create(:client) } 

    before(:each) do 
    visit new_client_path 
    fill_in "client_name", with: client.name 
    fill_in "client_address1", with: client.address1 
    fill_in "client_city", with: client.city 
    fill_in "client_state", with: client.state 
    select client.country, from: "client_country" 
    fill_in "client_zipcode", with: client.zipcode 
    fill_in "client_contactname", with: client.contactname 
    fill_in "client_email", with: client.email 
    end 

    it "should create a client" do 
    expect { click_button "Create Client" }.to change(Client, :count) 
    end 

    describe "success messages" do 
    before { click_button "Create Client" } 
    it { should have_content('created') } 
    end 
end 

感謝

+0

不FactoryGirl已經將其保存到數據庫? –

+0

是的,它應該。爲了剛剛生成的屬性,應該使用'FactoryGirl.build(...)' – khustochka

回答

1

沒有什麼不對您的代碼。縮短它的唯一途徑我看到的是,雖然迭代的數組:

%w(name address1 city state zipcode contactname email).each do |field| 
    fill_in "client_#{field}", with: client.send(field.to_sym) 
end 

但這將只在非常簡單的例子,一個類型的字段(如你所見,我跳過client_country)。

因此,有是如何映射模型的任務沒有通用的解決方案屬性表單域,因爲表單字段可以是不同類型的,和映射可能更爲複雜。

+0

感謝。我會用'fill_in'去爲每個字段,爲清楚起見:) –