0

我有一个应用程序,用户可以发布并为其他帖子投票。在使用 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 是通过 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
4

2 回答 2

1

我假设您使用的是 RackTest 以外的驱动程序(基于您提到的 ajax,您需要某种 javascript 支持)。如果是这样,您可能没有正确设置 Capybara 以与 selenium 驱动程序共享数据库。有关更多详细信息,请参阅此 SO 答案此博客文章自述文件的这一部分

于 2013-06-13T21:36:32.337 回答
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

于 2013-06-15T13:27:50.003 回答