Reputation: 568
So, I am testing my Ruby on Rails project, but I am having a problem with the database.
I am using Stripe, and I am running integration tests for each of the pages along the user's purchasing experience. After a run through of the tests, my database is not rolling back, though!
It's very frustrating, because after each test I need to run rake db:test:prepare
, and that is slowing me down.
Below is the code for my spec:
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
end
If I start by running rake db:test:prepare
and then run my tests, everything is great. However, when I go to run the tests again, I get "Email already taken" validation errors on my User model since the database was not rolled back.
What am I doing wrong, here?
Upvotes: 1
Views: 757
Reputation: 7101
Note that while :rack_test is the default driver used by Capybara for regular tests, javascript tests are executed using a different driver (:selenium), which does not support transactional fixtures (without some workaround).
If that does not resolve the problem, nesting of javascript blocks within non-javascript blocks (especially interactive steps like visit/click/fill) can have erratic results in my experience, so I would avoid doing that.
As an aside, sleep is not a good idea and you might want to consider wait_until
and wait_for_ajax
.
Upvotes: 3
Reputation: 5398
Make sure you have
config.use_transactional_fixtures = true
in hour spec/rspec_helper.rb
Also, not sure what code you omitted in your example, but anything you are creating in before(:all)
you need to clean up yourself in after(:all)
, cause this is invoked before a transaction is opened.
Upvotes: 1