我正在使用 Capybara 进行一些集成测试,但在运行测试时遇到了问题。似乎我的设置在测试之前没有运行,或者至少在运行的第二次测试之前没有运行。以下是我到目前为止的测试:
require 'test_helper'
class UserSignupTest < ActionDispatch::IntegrationTest
setup do
FactoryGirl.create(:user_role)
end
test "user organic signup" do
visit new_user_registration_path
assert page.has_content?('Sign up for Connectedtrips')
fill_in('Email', :with => "hugo@gmail.com")
fill_in('First name', :with => "Hugo")
fill_in('Last name', :with => "KH")
choose("is_teacher_no")
choose("owns_center_no")
fill_in("Password", :with => "password")
fill_in("Password confirmation", :with => "password")
click_button("Sign up")
assert page.has_content?("Your registration is complete - thanks for joining!")
end
test "teacher organic signup" do
visit new_user_registration_path
assert page.has_content?('Sign up for Connectedtrips')
fill_in('Email', :with => "another@gmail.com")
fill_in('First name', :with => "Another")
fill_in('Last name', :with => "")
choose("is_teacher_yes")
choose("owns_center_no")
fill_in("Password", :with => "password")
fill_in("Password confirmation", :with => "password")
click_button("Sign up")
assert page.has_content?("Your registration is complete - thanks for joining!")
end
end
这是我的测试助手中的代码。我尽量设置它,我不确定是否需要数据库清理器,因为我使用的是 Datamapper 而不是 Active Record,因为我不知道这是否会有所作为。
ENV["RAILS_ENV"] = "test" 需要 File.expand_path('../../config/environment', FILE ) 需要 'rails/test_help' 需要 'capybara/rails' 需要 'database_cleaner'
module ActionController
class IntegrationTest
include Capybara::DSL
DataMapper.auto_migrate!
def teardown
DatabaseCleaner.clean
Capybara.reset_sessions!
Capybara.use_default_driver
destroy_all
end
def destroy_all
# destroying all my tables here
end
end
end
这是我收到的错误:
UserSignupTest
PASS test_teacher_organic_signup (0:00:00.929)
ERROR test_user_organic_signup (0:00:00.998)
ERROR: insert or update on table "users" violates foreign key constraint "users_role_fk"
DETAIL: Key (role_id)=(1) is not present in table "roles".
我不太确定如何解决这个问题,我将不胜感激,谢谢。