【问题标题】:Capybara actions not affecting test databaseCapybara 操作不影响测试数据库
【发布时间】:2013-06-13 21:04:45
【问题描述】:

我有一个应用程序,用户可以发帖并为其他帖子投票。在使用 Capybara 进行测试时,我无法正确执行操作。

我已经尽可能简化了我的测试,试图轻松找到错误,但无济于事:

请求/users_spec.rb

require 'spec_helper'

describe "Users" do

  before :all do
    Warden.test_reset!
    Warden.test_mode!
  end

  describe "when logged in, CAN" do
    before :all do
      User.all.map(&:destroy)
      Post.all.map(&:destroy)
      @user = User.create!(email: "foo@bar.baz",
                      username: "foobar",
                      password: "foobar",
                      password_confirmation: "foobar",
                      confirmed_at: Time.now - 1.minute)
      @user2 = User.create!(email: "foo2@bar.baz",
                      username: "foobar2",
                      password: "foobar",
                      password_confirmation: "foobar",
                      confirmed_at: Time.now - 1.minute)
      @post = Post.create!(
            title: Faker::Lorem.sentence(5),
            content: Faker::Lorem.sentence(25),
            post_type: "temporary",
            user_id: @user2.id
         )
      vote = LoggedUpvote.create!(user_id: @user2.id, post_id: @post.id)
      login_as(@user, scope: :user)
    end

    it "create Upvotes", js: true do
      count = Upvotes.all.count
      visit post_path(@post.obfuscated_id)
      page.find('.upvote').click
      Upvotes.all.count.should eq(count + 1)
    end  
  end
end

在我的 spec_helper.rb 文件中,我有:

Capybara.run_server = true 
Capybara.server_port = 7000
Capybara.app_host = "http://localhost:#{Capybara.server_port}"

Capybara 可以正常登录、导航和执行操作,但未创建赞成票的数据库记录(尽管显示确认已创建并且正确的用户已登录)。

通过ajax提交upvote,屏幕上显示成功响应,但数据库中没有创建记录。

如何获取要创建的记录?

编辑我的 spec_helper 文件如下所示:

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/rspec'
require 'capybara/rails'
require 'rubygems'
require 'spork'

include Warden::Test::Helpers

# From http://stackoverflow.com/questions/8662554/how-to-use-the-test-database-with-capybara
Capybara.run_server = true 
Capybara.server_port = 7000
Capybara.app_host = "http://localhost:#{Capybara.server_port}"

Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

Spork.prefork do
  RSpec.configure do |config|
    config.use_transactional_fixtures = false
    config.infer_base_class_for_anonymous_controllers = false

    config.order = "random" # From http://railscasts.com/episodes/413-fast-tests
    config.include FactoryGirl::Syntax::Methods

    config.treat_symbols_as_metadata_keys_with_true_values = true
    config.filter_run focus: true
    config.run_all_when_everything_filtered = true

    config.filter_run_excluding :slow unless ENV["SLOW_SPECS"]

    config.before(:all) { DeferredGarbageCollection.start }
    config.after(:all) { DeferredGarbageCollection.reconsider }

    config.before(:suite) { DatabaseCleaner.strategy = :truncation }
    config.before(:each) { DatabaseCleaner.start }
    config.after(:each) { DatabaseCleaner.clean }
  end
end

Spork.each_run do
  FactoryGirl.reload
end

【问题讨论】:

    标签: ruby-on-rails-3 rspec capybara integration-testing


    【解决方案1】:

    我假设您使用的是 RackTest 以外的驱动程序(基于您提到的 ajax,您需要某种 javascript 支持)。如果是这样,您可能没有正确设置 Capybara 以与 selenium 驱动程序共享数据库。有关详细信息,请参阅this SO answerthis blog postthis part of the README

    【讨论】:

    • 感谢您提供的资源,但根据这些页面,一切似乎都正常;我会将设置信息添加到我的问题中。
    • 这完全解决了我遇到的问题。问题是使用 selenium 并且没有使用 config.use_transactional_fixtures = falsedatabase_cleaner gem 配置 RSpec。
    【解决方案2】:

    事实证明,数据库设置很好,但 Capybara 没有正确等待 AJAX 事务完成。

    解决方案是在 Upvote 点击之后和测试断言之前添加以下内容:

    require "timeout"
    Timeout.timeout(Capybara.default_wait_time) do
      sleep(0.1) until Upvote.all.count == count + 1
    end
    

    现在一切正常。

    来源:https://gist.github.com/jnicklas/d8da686061f0a59ffdf7

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-09
      • 1970-01-01
      相关资源
      最近更新 更多