1

そのため、Ruby on Railsプロジェクトをテストしていますが、データベースに問題があります。

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、テストを実行すると、すべてが素晴らしいです。ただし、テストを再度実行すると、データベースがロールバックされていないため、ユーザーモデルで「Emailalreadytaken」検証エラーが発生します。

ここで私は何を間違っているのですか?

4

2 に答える 2

3

:rack_testはCapybaraが通常のテストに使用するデフォルトのドライバーですが、javascriptテストは、トランザクションフィクスチャをサポートしない別のドライバー( :selenium)を使用して実行されることに注意してください(回避策はありません)。

それでも問題が解決しない場合は、JavaScript以外のブロック内にJavaScriptブロックをネストすると(特にvisit / click / fillなどのインタラクティブな手順)、エクスペリエンスが不安定になる可能性があるため、これは避けます。

余談ですが、睡眠は良い考えではないので、検討することをお勧めしwait_untilますwait_for_ajax

于 2012-11-23T17:16:26.833 に答える
1

あなたが持っていることを確認してください

config.use_transactional_fixtures = true

時間でspec/rspec_helper.rb

また、例で省略したコードはわかりませんが、作成するコードはすべてbefore(:all)自分でクリーンアップする必要がありますafter(:all)。これは、トランザクションが開かれる前に呼び出されるためです。

于 2012-11-23T14:14:30.980 に答える