2012-04-22 136 views
0

我有這樣爲什麼發生這種情況與黃瓜測試?

Given /^initial data$/ do |table| 
    @schedule_05 = Factory.build :schedule_05 
    @schedule_05.save 
    puts "count of rows #{@schedule_05.cycles.count}" - # is 7 
end 
#....... there are same steps and where filling text fields and so on/ 

When /^i click a submit button "([^"]*)" in form$/ do |runs| 
    click_button(runs) # this is ajax request 
end 

Then /^count of SchOfWorkInformation is 365$/ do 
    puts "count of rows #{ScheduleOfWorking.count}" # is - 1 
    s_c = ScheduleOfWorking.find_by_schedule_code("05") # is too 1 
    puts "05 schedule is presents #{s_c.blank?}" # false 
    unless s_c.blank? 
    puts "05 schedule is presents #{s_c.date_of_countings.blank?}" #false 
    end 
end 

步驟,當我點擊提交方案涉及控制器ScheduleOfWorkingController的填充作用

def filling 
unless request.xhr? 
    @classifier_schedule = ScheduleOfWorking.classifiers_numbers 
    @schedule_number = ScheduleOfWorking.classifiers_numbers false 
else 
    if params[:date_begin].blank? || params[:date_end].blank? 
    render :js => "alert('date fields is empty !')" 
    return 
    end 
    ScheduleOfWorking.fill_information_for(params[:date_begin].to_date,params[:date_end].to_date) 
end 

ScheduleOfWorking#fill_information_for

def self.fill_information_for(date_begin, date_end) 
    sch = all 
    sch.each do |e_sch| 
    e_sch.date_of_countings.each do |d_counting| 
     h_calculate = d_counting.roll_cycle(date_begin, date_end) 
     transaction do 
     SchOfWorkInformation.delete_all("(date >= '#{date_begin}' and date <= '#{date_end}') and schedule_code = #{d_counting.sch_code}") 

     SchOfWorkInformation.create! h_calculate 
     end 
    end 
    end 
end 

在SchOfWor k信息我有這樣的自定義驗證方法

def customer_validate 
s = SchOfWorkInformation.where(:date => self.date, :schedule_code => self.schedule_code).first 

if !s.blank? && (new_record? || s.id != self.id) 
    self.errors[:base] = "Запись не уникально!(date=#{self.date.to_s(:db)}, schedule_code=#{self.schedule_code})" 
end 

sch_number = schedule_code[0..1].blank? ? "0" : schedule_code[0..1] 
session_number = schedule_code[2].blank? ? "0" : schedule_code[2] 
logger.info "---------------------------------------" 
ss_a = ScheduleOfWorking.all 

s = ScheduleOfWorking.where("schedule_code='#{sch_number}'"). 
         joins(:date_of_countings). 
         where("date_countings.session_number=#{session_number}") 
    # In Given section of my step i load the data, but s.balnk? gives true 
    if s.blank? 
    self.errors[:base] = "Schedule - #{sch_number} with session number #{session_number}), is not exists in DB" 
    end 
end 

爲什麼我的數據在驗證方法中丟失?請幫助!

在開發環境。所有的權利,但在測試不。

..sorry長信..

您使用的這些測試

回答

1

什麼數據庫策略?如果它在事務中運行,則數據永遠不會實際持久保存到數據庫中。您應該使用其他方法執行這些步驟,因爲無論有多少個線程或進程正在運行,數據都將保持持久。當你發出一個ajax請求時,它在一個到服務器的單獨請求中,並且數據庫事務只能用於該請求。

在我的申請,我有使用共享連接的JavaScript測試設置:

Cucumber::Rails::Database.javascript_strategy = :shared_connection 
+0

我經過黃瓜::滑軌:: Database.javascript_strategy =:shared_connection在env.rb,但現在我得到Mysql2 ::錯誤:此連接仍在等待結果,請在結果後再試一次:在我的服務器日誌中。 – dilshod 2012-04-22 18:27:22

+1

這是因爲ajax請求是異步的,並且在完成reqeust之前黃瓜移動到下一步。您有幾個選項,包括在轉到下一步之前等待事件完成或在頁面上定位某個元素並強制它等待。 – 2012-04-22 19:12:44

+0

下面是一個示例http://pivotallabs.com/users/mgehard/blog/articles/1671-waiting-for-jquery-ajax-calls-to-finish-in-cucumber或查看AJAX部分了解更多信息https ://github.com/jnicklas/capybara – 2012-04-22 19:13:27

相關問題