gpt4 book ai didi

ruby-on-rails - RSpec、Capybara、JavaScript 测试未正确回滚数据库

转载 作者:太空宇宙 更新时间:2023-11-03 17:56:01 24 4
gpt4 key购买 nike

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

我在这里做错了什么?

最佳答案

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

如果这不能解决问题,在非 javascript block 中嵌套 javascript block (尤其是像访问/点击/填充这样的交互步骤)在我看来可能会产生不稳定的结果,所以我会避免这样做。

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

关于ruby-on-rails - RSpec、Capybara、JavaScript 测试未正确回滚数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13530619/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com