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