2012-11-23 28 views
1

所以,我測試我的Ruby on Rails項目,但我有一個數據庫的問題。RSpec,水豚,JavaScript測試不正確回滾數據庫

我正在使用Stripe,並且正在爲用戶的購買體驗中的每個頁面運行集成測試。但是,在完成測試後,我的數據庫不會回滾,不過!

這非常令人沮喪,因爲在每次測試後我都需要運行rake db:test:prepare,這會讓我放慢腳步。

下面是我的規格代碼:

require 'spec_helper' 

describe "Subscription Pages" do 
    subject { page } 

    ... 


    describe "purchase page" do 

    ... 

    describe "when not signed in" do 
    it { should have_field "Email" } 
    it { should have_field "Full Name" } 
    it { should have_field "Password" } 
    it { should have_field "Confirmation" } 

    describe "and fills in the provided sign-up form as new user" do 
     let(:some_user){ FactoryGirl.build(:user) } 
     before do 
     fill_in "Full Name",    with: some_user.name 
     fill_in "Email",     with: some_user.email 
     fill_in "Password",    with: some_user.password 
     fill_in "Confirmation",   with: some_user.password 
     fill_in "card_number",   with: "4242424242424242" 
     fill_in "card_code",    with: "123" 
     select "January", from: "card_month" 
     select (Date.today.year+1).to_s, from: "card_year" 
     end 

     ... 

     describe "and submits with valid information", js: true do 
     before do 
      click_button submit 
      sleep 5 
     end 

     it { should have_selector('div.alert.alert-success', text: 'Thank you for purchasing!') } 
     it { should have_field "Words" } # because its the new puzzle page. 
     it { should_not have_link "Buy Now!", href: plans_path } 

     describe "and then tries to revisit the purchase page", js: true do 
      before do 
      visit purchase_path 
      end 

      # Should redirect to account screen with an alert 
      # These tests pass when I do them myself but don't know why they are failing here 
      specify { User.find_by_email(some_user.email).subscription.paid_user?.should be_true } 

      it { should have_selector('title', text: "Account") } 
      it { should_not have_field "card_number" } 
      it { should have_selector('div.alert.alert-notify') } 
      it { should_not have_link "Buy Now!", href: plans_path } 
     end 
     end 
    end 
    end 
end 

如果我通過運行rake db:test:prepare啓動,然後運行我的測試中,一切都很好。但是,當我再次運行測試時,由於數據庫未回滾,因此我的用戶模型中出現「已發送電子郵件」驗證錯誤。

我在做什麼錯,在這裏?

+0

你正在使用哪種水豚驅動程序? –

+0

我相信它的硒 - 但我該如何檢查確定?我不知道100%,並不知道在哪裏檢查... – csalvato

+0

Capybara.current_session.driver(在一個規格內)會告訴你目前的驅動程序是什麼 –

回答

3

請注意,雖然:rack_test是水豚用於常規測試的默認驅動程序,但JavaScript測試使用不同的驅動程序(:selenium)執行,其中does not support transactional fixtureswithout some workaround)。

如果這樣不能解決問題,那麼在非JavaScript塊(特別是像visit/click/fill這樣的交互式步驟)中嵌套javascript塊可能會導致我的經驗不穩定,所以我會避免這樣做。

另外,睡眠不是一個好主意,你可能要考慮wait_untilwait_for_ajax

+0

Awesome Prusswan。將不得不考慮這兩個鏈接,並會接受答案,如果他們的工作。 (明天或第二天可能會這樣做......) 關於'wait_until'和'wait_for_ajax',我無法讓他們工作,並且我有一個最後期限要見面,而且睡眠完成了這項工作。我計劃重構,現在的最後期限已經過去了...... – csalvato

+0

要明確,[此鏈接](http://stackoverflow.com/questions/8774227/why-not-use-shared-activerecord-connections-for- rspec-selenium)是我嘗試過的。花了2秒鐘,一切都很好。 – csalvato

1

請確保您有

config.use_transactional_fixtures = true 

在小時spec/rspec_helper.rb

而且,不知道你在你的例子遺漏了什麼代碼,但任何你正在創建before(:all)你需要清理自己after(:all),因爲這是在事務打開之前調用的。

+0

我沒有任何'之前(:所有)'或':after(:all)',和 config.use_transactional_fixtures = true 在'spec/spec_helper.rb'中設置 任何其他想法? – csalvato