2012-06-16 21 views
0

我運行以下特點:我的FactoryGirl創建的對象不會持久?

Scenario: viewing existing images 
    Given I am on the images page 
    And 4 images already exist 
    Then I should see a table containg those 4 images 
    And have the option to show images 
    And have the option to delete images 

與定義下列步驟操作:

Given /^I am on the images page$/ do 
    visit(images_path) 
end 

Given /^(\d+) images already exist$/ do |count| 
    count.to_i.times { 
    FactoryGirl.build(:image).save! 
    } 
end 

Then /^I should see a table containg those (\d+) images$/ do |count| 
    page.all('table#imagesTable tr').count.should == count 
end 

的最後一步,在飯桌數行,悲慘的失敗了。它只能找到一行,我認爲它是標題行。這些是索引頁面的測試,我已經手動確認它的工作原理。爲什麼我的FactoryGirl創建的對象沒有拿起我的控制器?

控制器的指數方法:

def index 
    @images = Image.all 
    end 
+0

嘗試更換的順序「我的圖片頁面上」和'4個圖像已經存在'給予。我認爲索引操作在創建圖像之前被調用,因此新圖像實際上並沒有被拾取 – SuperMaximo93

+0

@ SuperMaximo93乾杯,你釘了它。提交一個答案,如果你想信用:) – willcodejavaforfood

+0

酷:)提交回答 – SuperMaximo93

回答

3

交換的「我的圖片頁面上」和「4個圖像已經存在」吉文斯的順序。

index操作被稱爲創建圖像之前呈現的觀點,所以他們就不會被拾起。

另外,我不知道你是否知道這一點了,但不是

FactoryGirl.build(:image).save! 

你可以做

FactoryGirl.create(:image) 
+1

已經做到了。在閱讀文檔時你會學到很多東西:) – willcodejavaforfood

+0

我試圖調試一個測試,浪費了3-4個小時,僅僅因爲我使用'build',除了'create'外。我想,一個閱讀文檔是不夠的:) –