Capybara行动不影响测试数据库

时间:2013-06-13 21:04:45

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

我有一个应用程序,用户可以发布并投票支持其他帖子。在与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登录很好,导航很好,并且执行操作很好,但是没有创建upvote的数据库记录(尽管显示已经创建并且正确的用户已登录的视觉确认)。

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

如何创建记录?

编辑我的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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

事实证明,数据库设置很好,但是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