2015-07-03 74 views
1

我想完成爲我的應用編寫單元和集成測試,但是當我運行耙測試時,我看到很多錯誤,所有這些都是由系統嘗試刪除這是隻讀的SQL視圖。爲什麼當測試只是爲了驗證數據創建以及如何讓我的測試過去時,它試圖刪除視圖?該應用程序工作得很好,但我無法讓測試正常運行。 (在添加sql視圖之前,所有測試都正常運行)。Rails測試試圖刪除sql視圖

這是我看到的錯誤:

Finished in 0.779585s, 39.7647 runs/s, 0.0000 assertions/s. 

    1) Error: 
UsersControllerTest#test_should_get_new: 
ActiveRecord::StatementInvalid: PG::ObjectNotInPrerequisiteState: ERROR: cannot delete from view "playabilitysummary" 
DETAIL: Views that do not select from a single table or view are not automatically updatable. 
HINT: To enable deleting from the view, provide an INSTEAD OF DELETE trigger or an unconditional ON DELETE DO INSTEAD rule. 
: DELETE FROM "playabilitysummary" 


Error: 
UsersControllerTest#test_should_get_new: 
NoMethodError: undefined method `each' for nil:NilClass 



    2) Error: 
StaticPagesControllerTest#test_should_get_contactus: 
ActiveRecord::StatementInvalid: PG::ObjectNotInPrerequisiteState: ERROR: cannot delete from view "playabilitysummary" 
DETAIL: Views that do not select from a single table or view are not automatically updatable. 
HINT: To enable deleting from the view, provide an INSTEAD OF DELETE trigger or an unconditional ON DELETE DO INSTEAD rule. 
: DELETE FROM "playabilitysummary" 

PlayabilitySummary模型:

class PlayabilitySummary < ActiveRecord::Base 

self.table_name = 'playabilitysummary' 
    after_initialize :readonly! 

end 

可玩性摘要遷移:

class CreatePlayabilitySummaries < ActiveRecord::Migration 
    def up 
    execute <<-SQL 
    CREATE VIEW PlayabilitySummary AS 
    SELECT date,user_id,speed,firmness,moisture FROM (((SELECT distinct(schedules.date) as date, speed.user_id as user_id, speed.speed as speed FROM schedules 
join 
    (SELECT user_id, measure_date as speed_date, avg(speed) as speed FROM greens group by user_id, measure_date order by user_id, measure_date asc) as speed 
on schedules.date = speed.speed_date) as avg1 
JOIN 
    (SELECT user_id as fuser_id, measure_date as firm_date, avg(firmness) as firmness FROM greens group by user_id, measure_date order by user_id, measure_date asc) as firmness 
ON avg1.user_id = firmness.fuser_id AND avg1.date = firmness.firm_date) as avg2 
JOIN 
    (SELECT user_id as muser_id, measure_date as moist_date, avg(moisture) as moisture FROM greens group by user_id, measure_date order by user_id, measure_date asc) as moisture 
ON avg2.fuser_id = moisture.muser_id AND avg2.date = moisture.moist_date) 
     SQL 
    end 

def down 
    execute 'DROP VIEW PlayabilitySummary' 
end 

end 

樣品測試(user_controller):

require 'test_helper' 

class UsersControllerTest < ActionController::TestCase 
    test "should get new" do 
    get :new 
    assert_response :success 
    end 


end 

回答

0

此問題已通過創建rake db:views任務來創建sql視圖並從遷移中刪除這些問題來解決。這使得測試數據庫準備試圖刪除被定義爲read_only的表。

此外,db用戶被授予超級用戶訪問權限。這兩個步驟都已成功消除了刪除錯誤。

+0

謝謝,這裏有一篇文章解釋瞭如何做到這一點https://rietta.com/blog/2015/03/30/adding-rake-db-views-for-sql-views-to-a- rails-project /但我仍然得到相同的錯誤 – agusgambina