1

我有兩個失敗的測試,目前令我困惑。他們正在測試新會議的表單是否正確保存到數據庫中。該行爲在瀏覽器中起作用。這些測試正在測試通過兩條不同路線進入會議/新頁面的情況。由於「會議」屬於「組織」,因此可以從會議索引頁面(路由是new_admin_conference_path)並通過組織顯示頁面(其中路由是new_admin_organization_conference_path(org))添加新會議。Rails 4 minitest Object.last返回錯誤的結果,儘管object.save傳球

測試:

test "admin can create a conference through new_admin_conference_path" do 
    sign_in_admin 
    visit new_admin_conference_path 

    fill_in 'Name', :with => 'RailsConf' 
    select(organizations(:valid_org).name, :from => 'conference_organization_id') 

    assert_difference('Conference.count', 1) do 
     click_button 'Save' 
    end 

    assert_equal 'RailsConf', Conference.last.name, "field 'name' did not save to database" 
    assert_equal organizations(:valid_org).id, Conference.last.organization_id, "field 'organization_id' did not save to database" 
    end 

    test "admin can create a conference through new_admin_organization_conference_path" do 
    sign_in_admin 
    visit new_admin_organization_conference_path(organizations(:valid_org)) 

    fill_in 'Name', :with => 'EmberConf' 

    assert_difference('Conference.count', 1) do 
     click_button 'Save' 
    end 

    assert_equal 'EmberConf', Conference.last.name, "field 'name' did not save to database" 
    assert_equal organizations(:valid_org).id, Conference.last.organization_id, "field 'organization_id' did not save to database" 
    end 

它們與失敗的消息:

1) Failure: 
AdminUserFlowsTest#test_admin_can_create_a_conference_through_new_admin_conference_path [/Users/wendybeth/projects/confreaks/tv3/test/integration/admin_user_flows_test.rb:141]: 
field 'name' did not save to database. 
Expected: "RailsConf" 
    Actual: "Conference1" 


    2) Failure: 
AdminUserFlowsTest#test_admin_can_create_a_conference_through_new_admin_organization_conference_path [/Users/wendybeth/projects/confreaks/tv3/test/integration/admin_user_flows_test.rb:156]: 
field 'name' did not save to database. 
Expected: "EmberConf" 
    Actual: "Conference1" 

209 runs, 474 assertions, 2 failures, 0 errors, 0 skips 

其中 「Conference1」 是夾具會議的名稱(:valid_conf)。但是,assert_difference('Conference.count',1)正在傳遞。我已經在click_button保存之前和之後將Conference.count打印到了屏幕上,並且它是正確的(兩個測試分別爲1和2)。

我已經完成了save_and_open_page,每次測試都在click_button'Save'後,頁面分別正確加載到'RailsConf'顯示頁面和'EmberConf'顯示頁面,每個頁面都有相應的'會議已成功保存'注意。

對於與會議和視頻有關的事件,事件的事件我有類似的測試和功能 - 這些測試全部通過。我的想法是,這可能有一些做的測試數據庫是奇怪...我使用DatabaseCleaner這樣在測試文件的開頭:

before :each do 
    DatabaseCleaner.start 
    end 

    after :each do 
    DatabaseCleaner.clean 

    if page.has_content?('Sign Out') 
     click_link 'Sign Out' 
    end 
    end 

對於被徹底,路由着想。 rb文件:

namespace :admin do 
    ... 

    resources :organizations do 
    resources :conferences 
    end 

    resources :conferences 

    ... 
end 

其中'...'代表不相關的路線。

會議新創建的方法(用action_args):

def new organization_id = nil 
    @conference = Conference.new(organization_id: organization_id) 
    end 

    def create 
    @conference = Conference.new(conference_params) 

    if @conference.save 
     flash[:notice] = "'#{@conference.name}' has been created." 
     redirect_to admin_conference_path(@conference) 
    else 
     flash[:error] = "Unable to create the conference at this time." 
     render action: 'new', id: @conference 
    end 
    end 

唯一的半相關的搜索結果我發現一直在這裏:

Rails 3 "last" method is returning incorrect result from ActiveRecord output

我使用Rails 4,但是。我已經嘗試了Conference.order('created_at')。最後得到了相同的結果,並且由於它是一個Rails 3 ActiveRecord錯誤,這令人沮喪地不是正確的方向。

可疑的是,在每個assert_difference方法之前和之後打印'Conference.last.name'時,儘管'RailsConf'和'EmberConf'被成功保存,但它會同時打印'Conference1'。

這是怎麼回事?

另注

這些測試通過當我第一次寫了他們。自從編寫更多代碼(主要是展示和索引頁面上的前端內容,與這裏處理的功能很少)之後,它們才失敗了。我通過將表單上的「提交」更改爲「保存」,打破了一系列測試,並修復了這兩個看似無關的失敗,這些失敗令人費解,令人沮喪。

回答

0

assert_difference測試正在通過,只是與Conference.last {field}的數據比較失敗。

檢查您的對象的default_scope,因爲該記錄似乎基於傳遞assert_difference正確保存。