【问题标题】:RSpec, Capybara, JavaScript Tests Not Properly Rolling back the DatabaseRSpec、Capybara、JavaScript 测试未正确回滚数据库
【发布时间】:2012-11-23 14:00:42
【问题描述】:

所以,我正在测试我的 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 然后运行我的测试,一切都很好。但是,当我再次运行测试时,由于数据库没有回滚,我的用户模型上出现“电子邮件已接收”验证错误。

我做错了什么,在这里?

【问题讨论】:

  • 你用的是哪个水豚驱动?
  • 我相信它的 Selenium - 但我怎样才能确定呢?我不知道 100% 也不知道在哪里检查...
  • Capybara.current_session.driver(在规范中)会告诉你当前的驱动程序是什么

标签: ruby-on-rails ruby rspec ruby-on-rails-3.2 capybara


【解决方案1】:

请注意,虽然 :rack_test 是 Capybara 用于常规测试的默认驱动程序,但 javascript 测试是使用不同的驱动程序 (:selenium) 执行的,即 does not support transactional fixtures (without some workaround)。

如果这不能解决问题,在我的经验中,将 javascript 块嵌套在非 javascript 块中(尤其是诸如访问/点击/填充之类的交互式步骤)可能会产生不稳定的结果,所以我会避免这样做。

顺便说一句,睡眠不是一个好主意,您可能需要考虑wait_untilwait_for_ajax

【讨论】:

  • 很棒的普鲁士旺。必须查看这两个链接,如果它们有效,将接受答案。 (可能会在明天或后天的某个时候这样做......)关于wait_untilwait_for_ajax,我无法让他们工作,而且我有一个截止日期,并且睡眠完成了这项工作。现在截止日期已经过去了,我计划重构...
  • 明确地说,this link 是我尝试过的。花了 2 秒钟,一切都很好。
【解决方案2】:

确保你有

config.use_transactional_fixtures = true

小时内spec/rspec_helper.rb

此外,不确定您在示例中省略了哪些代码,但您在 before(:all) 中创建的任何代码都需要在 after(:all) 中自行清理,因为这是在打开事务之前调用的。

【讨论】:

  • 我没有before(:all)after(:all),并且spec/spec_helper.rb中设置了config.use_transactional_fixtures = true还有其他想法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多